简体   繁体   English

为什么我的数组返回的是对象而不是数字?

[英]Why my arrays are returning object instead of numbers?

I am trying to find Least Common Multiple.我正在尝试找到最小公倍数。 But when I tried to find the final result, I got NaN and realized both of my arrays are returning object.但是当我试图找到最终结果时,我得到了NaN并意识到我的两个数组都在返回对象。 Sharing the code below -分享下面的代码 -

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;
}

Use "Math.min(...filtArray);"使用“Math.min(...filtArray);” instead.反而。 We use the spread operator ... when calling the Math.min method.我们在调用 Math.min 方法时使用扩展运算符 ...。

Since the Math.min method expects comma separated numbers as arguments, we can't directly pass it an array.由于 Math.min 方法需要以逗号分隔的数字作为参数,因此我们不能直接将其传递给数组。

This works like a charm这就像一个魅力

 <!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>

Least common multiple 6,12,18,24,30,36,42,48,54,60 are the multiples of 6最小公倍数 6,12,18,24,30,36,42,48,54,60 是 6 的倍数

3,6,9,12,15,18,21,24,27,30 are the multiples of 3 3,6,9,12,15,18,21,24,27,30 是 3 的倍数

Common multiples 6,12,18,24,30公倍数 6,12,18,24,30

6 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM