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. 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);" instead. We use the spread operator ... when calling the Math.min method.
Since the Math.min method expects comma separated numbers as arguments, we can't directly pass it an array.
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
3,6,9,12,15,18,21,24,27,30 are the multiples of 3
Common multiples 6,12,18,24,30
6