Continuing the discussion from freeCodeCamp Challenge Guide: Smallest Common Multiple:

I am studying the solution to get better at solving this problem, but this while loop has me confused. What is the loop doing for each iteration?

Continuing the discussion from freeCodeCamp Challenge Guide: Smallest Common Multiple:

I am studying the solution to get better at solving this problem, but this while loop has me confused. What is the loop doing for each iteration?

`while ((num % i) === 0)`

=> while numer is even

The line `factors[i] = (factors[i]) ? factors[i] + 1 : 1;`

uses a ternary operator to increment the value of `factors[i]`

by 1 if it already exists (i.e., it is truthy), or set it to 1 if it doesnâ€™t exist yet (i.e., it is falsy). This line effectively counts the number of times â€śiâ€ť appears as a factor of â€śnumâ€ť.

`num /= i;`

devides `num`

by itâ€™s index

1 Like

given whats already been said, here is my two bits

- keeps track of occurrence of factor, as in which number is just matched
`num % i === 0`

- if you recall, any loop requires its â€śiterator - as in whats next to work withâ€ť, this here also does that same by "dividing it by its â€śpreceding loop indexâ€ť, so that this â€śwhileâ€ť loop can come to an end at some point

hope this was helpful, happy learning

I would only look at the solutions once you have an answer of your own. Itâ€™s easier to see what the solutions do in that case.

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