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[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…
// 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?
// 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
}