 # Smallest Common Multiple - LCM-ing like in math?

Smallest Common Multiple - LCM-ing like in math?
0

#1

Tell us what’s happening:

I wrote comments on what it supposed(for me heh) … what i wanted from this code, basically i wanted to kind of copy real LCM operation? Starting from 2 and checking if given num from array is divisible of that num (i)…
If true, adds that num to new array and divide every num by (i) and so on till every num from sortedArr gets devided to 1? and then just multiply it with reduce? And btw it does work(checks) for 1-5 range when lcmArr.push(i) is last thing in last if statment, tho doesnt really makes sense to me why. Any hints where i could go with this, i really dont want to check any solutions(feeling like im kinda close with my own?)
Oh and btw, for example for first one (1-5) console lcmArr returns [2,3,5]… it does miss that second 2… not really sure why, i mean i figure out by consoling sortedArr that what was 4 is now 2 and not 1 at the end… Shouldn’t array.some() and loop after it take care of that, by dividing it untill there is no more to be divided by (i)?

Your code so far

``````
function smallestCommons(arr) {

// Sorted Array and needed variables
let sortedArr = [];
let holderArr = arr.sort((a, b) => a - b);
for (let i = holderArr; i <= holderArr; i++) {
sortedArr.push(i);
}
let lcmArr = [];

//Iterating over sortedArr to check if any num in arr is div by current number from //loop, if true, push that num into new arr, and div every divisible num by that
// number. Keep checking it till there isnt num in arr to div with it, go to next num
for (let i = 2; i <= sortedArr[sortedArr.length-1]; i++) {
if (sortedArr.some(num => num % i === 0)) {
lcmArr.push(i);
for (let j = 0; j < sortedArr.length; j++) {
if (sortedArr[j] % i === 0) {
sortedArr[j] = sortedArr[j] / i;
}
}
}
}
console.log(lcmArr);
return lcmArr.reduce((a,b) => a * b);

}

smallestCommons([1,5]);

// for (let num = 2; true; num++) {
//       if (fullArr.every(x => num % x === 0)) {
//         console.log(num);
//         return num;
//       }
//     }

// for (let i = 0; i < sortedArr; i++) {

// var num = 2;
// while (true) {
//   if (sortedArr.every(x => num % x === 0)) {
//     console.log(num);

//     return num
//   }
//   num++;
// }
// }

// let check;
// for (let num = 2; true; num++) {
//   check = sortedArr.every(x => num % x === 0);
//   console.log(check);
//   if (check === true) {
//     return num;
//   }
// }
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36`.

#2

I’m going to talk to myself for a bit hih… but in case someone is checking my post… i changed it up a bit… using splice and while instead of if:

``````for (let i = 2; i <= sortedArr[sortedArr.length-1]; i++) {
while (sortedArr.some(num => num % i === 0)) {
lcmArr.push(i);
for (let j = 0; j < sortedArr.length; j++) {
if (sortedArr[j] % i === 0) {
sortedArr.splice(j, 1, (sortedArr[j] / i));
console.log(sortedArr);
}
}
}
}
``````

…And everything checks but [2, 10] … lcmArr of those returns (2, 2, 2, 3, 3, 5), 7… i am trying to figure out why… but in case someone checking my post just wanted to update it and not make someone lose time for no reason… : )

… it was because of [sortedArr.length-1]… had to sort it again beneath splicing…

Oh and ps. To anyone that might be looking for way to complete this challenge and stumbles up on my post… I spent 7-8 hours… this is 3rd day… Take notebook, and some time fight it thru, leave it, do something else… just dont copy solutions… think ahead of reward it will bring to you when you finally figure that shit out!.. Struggle today for tomorrow. And going back to elemntary school stuff helped me alot to understand what the hell i am supposed to do… Goodluck! You’ill figure that out!