smallestCommonMultiple algo syntax question

Hi guys.
So, I want that my arr to keep multiplying itself until a number is found in a new array which has no remainder in each number of the first array.

I have a program that multiples itself, and also have a program that check if a number has a remainder.

Now I want my isRemainder program to check if multipleArr has a remainder with every number in arr, and if not to multiply again, I am not sure how to write it down… Please assist : )

P.S. I have the while loop but I don’t know how to continue it.

``````   **Your code so far**
``````
``````function smallestCommons(arr) {
let endNum = arr[1];
let startNum = arr[0];
let rangeNums = endNum - startNum;
arr.sort();
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}
arr.sort();
console.log(arr)
const multiples = (num) => num + num;
const multipleArr = arr.map(multiples);
console.log(multipleArr)
const isRemainder = (num) => num % 2 == 0;
while (multipleArr) {

}
}

smallestCommons([1,5]);

/*
1 1 2 3 4 5 6 7 8 9 10 .. 60
2 4 6 8 10 12 14 16 18 20 .. 60
3 6 9 12 15 18 21 24 27 30 33 36 39 .. 60
4 8 12 16 20 24 28 32 36 40 44 48 52 56 60
5 10 15 20 25 30 35 40 45 50 55 60
2x1, 3x1, 2x2, 4x1, 5x1

find range
while (endNum - startNum !== startNum) {
endNum - startNum + 1

I want the arr to keep multiplying itself until a number is found that is divisible without leftover for every number in that previous array.
}
*/
``````
``````   **Your browser information:**
``````

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

Challenge: Smallest Common Multiple

Link to the challenge:

You may use Array.prototype.every() - JavaScript | MDN to check that “every” element satisfies some condition.

Is it okay to add a while loop inside a for loop?

In terms of js syntax? Yes, it’s ok. You may combine loops as you want.

Also, a few notes:

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

// suppose we have arr like [10, 3]
let endNum = arr[1];
let startNum = arr[0];

// next line will result in -7
let rangeNums = endNum - startNum;

// here you try to sort numeric array with the default sorting method
// this method won't give an expected result (look at the documentaction)
arr.sort();

// while (-7 !== 10)
//   -7--
// infinite loop
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}

// again incorrect sorting
arr.sort();
console.log(arr)
const multiples = (num) => num + num;

// can't get the logic behind the next line
const multipleArr = arr.map(multiples);
console.log(multipleArr)
const isRemainder = (num) => num % 2 == 0;
while (multipleArr) {

}
}
``````

I have added these lines:
now I know it’s not good enough yet, my problem is I want my checker to check remainder for every element in arr array, but how to do it? since I can’t access variables/arrays like objects.
I understand for syntax if I want to check arr elements I have to write it first, but I want every element in checker to check every element in arr… how to write that ;p? and while there is a remainder (false) then checker multiples.

``````const checker = [...arr]
while (checker.every(isRemainder) === false) {
checker.map(multiples);
}
}
``````

Let’s clarify the algorithm that you’re trying to implement:

1. we have a few numbers, and we want to find such a number that may be divided by every given number without remainder.
2. the most straightforward approach is to get the maximum number from our list of numbers and check if it can be divided by every other number without remainder.
3. if it can not be divided, then we increase it like: `curMult += maxNumber`
and check again if every other number from the list can be divided or not by curMult.
``````
1 2 3 4 5: max is 5 // max is 5, 5 can not be divided by all other numbers without remainder
1 2 3 4 5: 10 // false
1 2 3 4 5: 15 // false
1 2 3 4 5: 20 // false
and so on
1 2 3 4 5: 60 // ok - answer is 60
``````

Good day to you
This is what I have right now

``````function smallestCommons(arr) {
let endNum = arr[1];
let startNum = arr[0];
let rangeNums = endNum - startNum;
arr.sort();
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}
arr.sort();
console.log(arr)
let checker = arr.slice(-1)[0];
const isRemainder = (num) => num % 2 == 0;
//arr.every(isRemainder(checker));
for (let i =0; i < arr.length; i++) {
console.log(checker % arr[i] !== 0)
while (checker % arr[i] !== 0) {
checker = checker + arr.slice(-1)[0];
}
}
}
smallestCommons([1,5]);
``````

Don’t mind sort AT the moment, I’ll fix it when my algorithm works.

Now my second while loop isn’t doing what I want to do , since it only checks 3 of them since the rest pass… how can I fix it?

another question, I tried something using this code `//arr.every(isRemainder(checker));` but it is not doing what I wanted it to do, i wanted isRemainder function to take checker as parameter but it is not doing it, any way to re type the syntax so it will do it?

Ty

Good day

Analyze step by step what you’re doing here.

1. As the task states: `The range will be an array of two numbers that will not necessarily be in numerical order.` So, the endNum maybe smaller than the startNum.
So maybe you want to sort the arr prior to creating of those two vars.
2. `while (rangeNums !== startNum)`
This loop may go infinite. You need to fix the condition.
3. `arr.push(rangeNums);`
Here you don’t want to push this range.

but is it the problem right now? I want first to make sure my idea of algo works so at least the (1-5) range works
I mean this `smallestCommons([1,5]);`

P.S. I’ll take a look at it regardless

You seem to be lost in the tools and syntax.

Can you describe in words what your big picture plan for this function is? If the computer was instead a human, what would you be telling it to do?

if you are refering to my last question, then,

I want the highest number in the given range to be divided, and have no remainder. if it has a remainder i want the number to double itself and divide again until the first number that has no remainders.

Now I have tried to do it with the while loop and i understand why it doesn’t work and so on and on.
and also tried with the other command I wrote about. thanks

I don’t know what this means. Can you show an example with numbers?

1-5
1,2,3,4,5
5 highest
5/5-good
5/4-no
5/3-no
5/2-no
5/1-good

since not all
5+5 = 10
10/5 = ok
10/4 - no
10/3 no
10/2 ok
10/1 - no

since… …

What’s that next iteration? That might work, depending on what you do next.

``````for (let i =0; i < arr.length; i++) {
console.log(checker % arr[i] !== 0)
while (checker % arr[i] !== 0) {
checker = checker + arr.slice(-1)[0];
}
}
``````

problem in this that my while only checks 3, because 2 passes, so I don’t know how to go around that…

``````let checker = arr.slice(-1)[0];
const isRemainder = (num) => num % 2 == 0;
``````

I also tried this command
`//arr.every(isRemainder(checker));`
but I am not sure whether this syntax can be written and if so how? i want isRemainder function to use checker variable as a parameter.

You may or may not be replicating the logic you described. That’s why I wanted to see you do more steps with

where exactly am i replicating logic in my code?
So i iterate through ARR to get the single elements.
checker is the highest number in the range.
then my while has a condition to check if the highest number doesn’t have a remainder with each iteration…
since it is only 3 it only does the adding command i have next on only 3 of them.

so something about my condition is wrong but how can I make on every without using every method.
or this simple code that doesn’t work xD `//arr.every(isRemainder(checker));` is it possible to make it work on one line like I want to ?

Dude, can you give me a couple extra rounds of your logic here please?

10+ 5 = 15
15/5=k
15/4 n
15/3 k
15/2 n
15/1 k

15+5 20
20/5 k
20/4 k
20/3 n
20 / 2 k
20/ 1 k

25
25/5 k
25/4 n
25/3 n
25/2 n
25/1 k