smallestCommonMultiple algo syntax question

Ok, that is good logic, but that’s not what you are doing in your loops.

This says loop over array elements once. But you loop over the array elements many times in the process you’re describing.

so I loop through arr first, is it wrong? for (let i =0; i < arr.length; i++)

Yes. That explicitly says to only loop over the array once. You need to loop many times.

while (checker % arr[i] !== 0) doesn’t this condition tell it to do it “many” times?

That is inside of your loop that says to go through the array once. The order of nesting makes a difference.

but if u mean to use while (checker % arr[i] !== 0) this first I can’t swap them because i need the single element of arr

Your code doesn’t match your logic though.

How do you put this block into words? What is happening here? This is your ‘inner’ loop.

Your ‘outer’ loop adds “5” to the test value each time.

Does it have to be with your logic? Cant I make it work with my own?

my logic is the checker ( highest number ) has nothing to do with the loop condition. the addition I mean.

… I’m trying to make it with your logic. Your code does not match the logic you described.

in my own words.

highest number is added to the highest number, and those numbers check remainder with the range numbers.

So that is what your outer loop needs to do. It isn’t doing that right now.

so basically You mean I have to TYPE my FIRSt thing I want to DO

If you want it to do the logic you are describing, then yeah, you code has to line up with that.

In your logic, as I understand it, you are adding the big number to the number you are checking and then seeing if every number in the range divides into that number. What you are describing in words has an outer loop that ‘adds 5’ or whatever the biggest number is, and an inner loop that checks all values in the range, which you put in arr.

my question is in programming DO i Type the first thing I want to do not just here.
Because What I’ve did when I wrote my code, I understood Okay I need to LOOP so I LOOPED.

I’ll try to solve it, it is still vague for me but it makes sense to what another camper posted for me curMult += maxNumber

Also is it the only way to solve it with my own logic?
is it impossible to solve it while doing a for loop first?

It is possible to solve it with your logic. It is not possible to solve it with your logic written ‘backwards’.

1 Like

It might be a rhetorical question will I get to this level of knowledge by pracitcing and keep doing these algos?

Like for you it took you few seconds to understand my logic is backwards.

It takes practice. Coding is hard stuff.

Look here at the difference between these two pairs of loops:

const words = ["here", "are", "some", "words"];
const repeatNum = 3;

// Loop order 1
console.log("i outer, j inner");
for (let i = 0; i < words.length; i++) {
  for (let j = 0; j < repeatNum; j++) {
    console.log(words[i]);
  }
}

// Loop order 2
console.log("i inner, j outer");
for (let j = 0; j < repeatNum; j++) {
  for (let i = 0; i < words.length; i++) {
    console.log(words[i]);
  }
}

The order of the loops is very important.

You’re describing the right algorithm, it just isn’t coming out in the code the same way you are describing it in words.

on loop order 1, you repeat the word length 1 time am i correct?

and loop order 2 you repeat word order 4 times correct?