Estoy tratando de encontrar el mínimo común múltiplo. Pero cuando traté de encontrar el resultado final, obtuve NaN y me di cuenta de que mis matrices están devolviendo objetos. Compartiendo el código a continuación -
function myFunction(){ //For x var x = document.getElementById("int").value; let textx = ""; let multiplex = []; //For y var y = document.getElementById("int1").value; let texty = ""; let multipley = []; //result let result = ""; //FINDING THE MUTIPLES OF X if(isNaN(x)==false){ for (let i = 1; i < 11; i++) { if (i === 11) { break; } multiplex.push(((i*x)).toFixed(2)); } textx += multiplex + " are the multiples of "+x; }else{ window.alert("Please input a number. "+x+" is not a number") } document.getElementById("demo").innerHTML = textx; //FINDING THE MULTIPLES OF Y if(isNaN(y)==false){ for (let i = 1; i < 11; i++) { if (i === 11) { break; } multipley.push(((i*y)).toFixed(2)); } texty += multipley + " are the multiples of "+y; }else{ window.alert("Please input a number. "+y+" is not a number") } document.getElementById("demo1").innerHTML = texty; //Finding intersection and LCD const filtArray = multiplex.filter(value => multipley.includes(value)); console.log(Math.min(filtArray)) //Testing typeof console.group(typeof multiplex) //result object console.group(typeof multipley) //result object //result += Math.min(filtArray) + "is the Least Common Multiple (LCM)" //document.getElementById('result').innerHTML = result; }Utilice "Math.min(...filtArray);" en cambio. Usamos el operador de propagación... cuando llamamos al método Math.min.
Dado que el método Math.min espera números separados por comas como argumentos, no podemos pasarle directamente una matriz.
Esto funciona como un encanto
<!DOCTYPE html> <html> <body> <h1>JavaScript Numbers</h1> <h2>Least common multiple</h2> <p id="demo"></p> <p id="demo1"></p> <p id="demo2"></p> <p id="demo3"></p> <script> let num = 23; let n = num.toFixed(); var x = 6; let textx = ""; let multiplex = []; //For y var y = 3; let texty = ""; let multipley = []; //result let result = ""; //FINDING THE MUTIPLES OF X if(isNaN(x)==false){ for (let i = 1; i < 11; i++) { if (i === 11) { break; } multiplex.push(((i*x))); } textx += multiplex + " are the multiples of "+x; }else{ window.alert("Please input a number. "+x+" is not a number") } document.getElementById("demo").innerHTML = textx; //FINDING THE MULTIPLES OF Y if(isNaN(y)==false){ for (let i = 1; i < 11; i++) { if (i === 11) { break; } multipley.push(((i*y))); } texty += multipley + " are the multiples of "+y; }else{ window.alert("Please input a number. "+y+" is not a number") } document.getElementById("demo1").innerHTML = texty; //Finding intersection and LCD const filtArray = multiplex.filter(value => multipley.includes(value)); let textcommon = "Common multiples " + filtArray; document.getElementById("demo2").innerHTML = textcommon ; //Math.min(filtArray); document.getElementById("demo3").innerHTML = Math.min(...filtArray); console.log(Math.min(filtArray)) //Testing typeof console.group(typeof multiplex) //result object console.group(typeof multipley) //result object //result += Math.min(filtArray) + "is the Least Common Multiple (LCM)" //document.getElementById('result').innerHTML = result; //document.getElementById("demo").innerHTML = n; </script> </body> </html>Mínimo común múltiplo 6,12,18,24,30,36,42,48,54,60 son los múltiplos de 6
3,6,9,12,15,18,21,24,27,30 son múltiplos de 3
Múltiplos comunes 6,12,18,24,30
6