# smallestCommonMultiple algo syntax question

Output:

``````i outer, j inner
here
here
here
are
are
are
some
some
some
words
words
words
i inner, j outer
here
are
some
words
here
are
some
words
here
are
some
words
``````

THanks I used this https://pythontutor.com/
website and understood why loops like this, I got confused a bit i thought it will count each letter and connect them but every word is an element, nevermind xD.

Let’s try to break down the logic in smaller pieces Maybe that will work for understanding. Try to come up with the next two functions:

``````// finds and returns cm (common multiple)
function findCommonMultiple(arr) {
// here use isCommonMultiple function in a loop or a loop condition
// to check if current multiple is common
}

// returns true if every number in the array is divisible by cm
// otherwise return false
function isCommonMultiple(arr, cm) {
// here use loop and check numbers in the array
}

isCommonMultiple([1, 3, 5], 10) // false
isCommonMultiple([1, 2, 3], 6) // true
findCommonMultiple([1, 2, 3, 4, 5]) // 60
``````

hey,
in the second function
what do you mean by check numbers in the array
would you like me to do something like this

``````function isCommonMultiple(arr, cm) {
// here use loop and check numbers in the array
for (let i=0 ;i<arr.length;i++) {
console.log(cm % arr[i] == 0)
}
}
``````

hey. Yea, you may use this loop to check if the arr contains at least one divisor that divides with a remainder, and if you find it then return false. If you don’t find such a divisor in the loop, return true.

``````function smallestCommons(arr) {
arr.sort((a,b) => a-b);
let endNum = arr;
let startNum = arr;
let rangeNums = endNum - startNum;
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}
arr.sort((a,b) => a-b);
console.log(arr)
let checker = arr.slice(-1);
while (checker += arr.slice(-1) ) {
for (let i =0; i < arr.length; i++) {
if (checker % arr[i] !== 0) {
return false;
}
else {
return checker;
}
}
}
}
smallestCommons([1,5]);
``````

so I have a new code now.
Thing my addition stops at 10, now if my first “if” statement fails shouldn’t it go back to loop and keep increasing? or what can I do from here…

This is not a condition that will ever be false. You shouldn’t write an infinite loop.

Why are you returning `false`?

``````// finds and returns cm (common multiple)
function findCommonMultiple(arr) {
// here use isCommonMultiple function in a loop or a loop condition
// to check if current multiple is common
while (isCommonMultiple === false) {
cm += arr.slice(-1);
}
}

// returns true if every number in the array is divisible by cm
// otherwise return false
function isCommonMultiple(arr, cm) {
// here use loop and check numbers in the array
for(let i =0; i <arr.length; i++) {
if (cm % arr[i] == 0) {
return true;
}
else {
return false;
}
}
}

isCommonMultiple([1, 3, 5], 10) // false
isCommonMultiple([1, 2, 3], 6) // true
findCommonMultiple([1, 2, 3, 4, 5]) // 60
``````

did you mean something like this from your example?

``````function smallestCommons(arr) {
arr.sort((a, b) => a - b);
let endNum = arr;
let startNum = arr;
let rangeNums = endNum - startNum;
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}
arr.sort((a, b) => a - b);
console.log(arr)
let checker = arr.slice(-1);
function isRemainder(arr, cm) {
for (let i = 0; i < arr.length; i++) {
if (cm % arr[i] == 0) {
return true;
}
else { return false; }
}
}
while (isRemainder === false) {
for (let i = 0; i < arr.length; i++) {
if (checker % arr[i] > 0) {
checker += arr.slice(-1);
}
}
}
}
smallestCommons([1, 5]);
``````

This is what I have I have used udaaff’s help as well, please give me your notes , ty ``````// finds and returns cm (common multiple)
function findCommonMultiple(arr) {
// first you want to find biggest number in the array
let max // = max number
let cm = max
while (/* here check if the cm is common multiple by calling isCommonMultiple func*/) {
// cm += arr.slice(-1);
// here we increas cm by max on every iteration
}
// and we want to return cm
}

// returns true if every number in the array is divisible by cm
// otherwise return false
function isCommonMultiple(arr, cm) {
for (let i = 0; i < arr.length; i++) {
// you immediately return a value on the first iteration,
// that is not correct
// you want to check here if cm % arr[i] gives remainder,
// and return return false in that case
if (cm % arr[i] == 0) {
return true;
}
else {
return false;
}
}
// if we're here then all divisions went without remainer,
// hence cm is a common multiple
}
``````
``````while (isCommonMultiple(arr,cm) === false)
``````

is this my while condition like this?

yes.
or you may just write it like this

``````while (isCommonMultiple(arr, cm)) { // there is no need to compare with boolean, as soon as function gives you a boolean already
``````

and don’t forget to use “not” operator
we want loop executes while (cm is not common mult)

``````function smallestCommons(arr) {
arr.sort((a, b) => a - b);
let endNum = arr;
let startNum = arr;
let rangeNums = endNum - startNum;
while (rangeNums !== startNum) {
arr.push(rangeNums);
rangeNums--;
}
arr.sort((a, b) => a - b);
console.log(arr)
let checker = arr.slice(-1)
const isRemainder = ((arr, checker) => {
for (let i = 0; i < arr.length; i++) {
if (checker % arr[i] === 0) {
return true;
}
else {
return false;
}
}
})
while (isRemainder(arr, checker)) {
checker += arr.slice(-1);
}
}
smallestCommons([1, 5]);
``````

So I have fixed it once again. so how come my while is an infinite loop?

where do you mean I should have placed the not operator?

`while (!isCommonMultiple(arr, cm)) {`

can you explain to me why I need to do it?

``````const isRemainder = ((arr, checker) => {
for (let i = 0; i < arr.length; i++) {
if (checker % arr[i] === 0) {
return true;
}
else {
return false;
}
}
})
``````

if I have this function shouldn’t it do it automatically for me?
Does checker become mutable or is it still ‘pure’?

so lets say something like this?
`if (cm % arr[i] > 0) {return false};`
`else if (cm % arr[i] === 0) {return true;}`

Style note

``````if (condition) doSomething;
``````

or

``````if (condition) {
doSomething;
}
``````

not

``````if (condition) {doSomething;}
``````

A return statement immediately stops a function. If that return statement is in the middle of a loop, absolutely no more loop iterations occur.

1 Like
``````const isRemainder = ((arr, checker) => {
let flagRemainder = false;
let flagNoRemainder = false;
for (let i = 0; i < arr.length; i++) {
if (checker % arr[i] > 0) {
flagRemainder = true;
}
else if (checker % arr[i] === 0) {
flagNoRemainder = true;
}
}
if (flagRemainder) {
return false;
}
else if (flagNoRemainder) {
return true;
}
})
``````

So I can use flags here I guess, something like this?

Thanks for this! Now I only need to fix my array sort.