smallestCommonMultiple algo syntax question

Let’s try to break down the logic in smaller pieces :face_with_monocle: 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[1];
 let startNum = arr[0];
 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)[0];
 while (checker += arr.slice(-1)[0] ) {
  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)[0];
  }
}

// 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[1];
  let startNum = arr[0];
  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)[0];
  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)[0];
      }
    }
  }
}
smallestCommons([1, 5]);

This is what I have I have used udaaff’s help as well, please give me your notes , ty :slight_smile:

// 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)[0];
   // 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[1];
  let startNum = arr[0];
  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)[0]
  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)[0];
  }
}
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’?

re-read my comments inside the isCommonMultiple function

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.

Fixed it, I managed to complete. it worked when there were 2 numbers like 1,5.

I want to say thank you both for help @udaaff and @JeremyLT I am really happy I completed this challenge :slight_smile:
Had tears of joy xD

1 Like

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