For loop gives undefined value

For loop gives undefined value
In the smallest common multiple challange, I created 2 arrays:

  • one where the range of numbers between the parameters in arr are listed (newArr),
  • and one where multiples of the bigest number in arr are listed (multipleArr) until the largest possible value which is the multiplication of all the numbers in the range.

When I loop through both arrays to find the multiple that can be divided with all the numbers in the range, I got undefined as result. Why is that?

  **The code**

function smallestCommons(arr) {
arr = arr.sort((a,b) => a-b);
var newArr = [];
for (let i=arr[0]; i<=arr[1]; i++){
newArr.push(i)};

console.log(arr);
console.log(newArr);

var upperBound = 1
for (let a = arr[0]; a <= arr[1]; a++) {
upperBound *= a
}
console.log(upperBound);

var multipleArr=[];
for (var j=arr[1]; j <= upperBound; j += arr[1]) {
multipleArr.push(j)
};
console.log(multipleArr)

for (var n = 0; n < multipleArr.lenght; n++) {
for (var m = 0; m < newArr.length; m++) {
  if (multipleArr[n] % newArr[m] === 0) {
   return multipleArr[n]
  }
}
}
}
console.log(smallestCommons([5,1]));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Smallest Common Multiple

Link to the challenge:

Typo in this line perhaps?

1 Like

Ahh, thank you! Now it gives a value, just not the good one, but I was able to handle that.

This probably isn’t what you want.

Yes, I changed that part to this:

for (let n = 0; n < multipleArr.length; n++) {
if (newArr.every(m => multipleArr[n] % m === 0)) {
return multipleArr[n]}
}
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.