Implement a Range-Based LCM Calculator - Implement a Range-Based LCM Calculator

Tell us what’s happening:

I need to use a function on every number of the array. The thing is : i don’t know how i am supposed to put that into code and if that can be done that way.

How should I try to use the .reduce() function to use an if inside ? Is it possible ?

Your code so far

function smallestCommons(arr){
  arr.sort((a, b)=>(a-b));
  let allNumArr = [];
  let i = arr[0];
  let val= arr[1];
  for(let i=arr[0]; i<=arr[1]; i++){
    allNumArr.push(i);
  }
  //I want to use allNumArr.reduce with a function like so:
  // logic if (val % a==0 && bal %  b==0)(return val) else(val++) 
  allNumArr.reduce((a, b) => if(val % a == 0 && val % b == 0){
    return val;
  }
  else{
    val++;
  })
}

smallestCommons([1, 5])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0

Challenge Information:

Implement a Range-Based LCM Calculator - Implement a Range-Based LCM Calculator

if you have a function LCM and you need to calculate LCM(a,b,c), that’s equal to LCM( LCM(a, b), c ), so you can totally use reduce in this (you don’t need to, but you can)

to use it here you need to understand really well how reduce work, what the parameters of the callback are, and how they change value during the various iterations

Tell us what’s happening:

I don’t get it : Why doesn’t my “while” loop execute to check for a common divider of num and num+1. It would without “&&”.

Your code so far

function smallestCommons(arr){
  let allNumArr = [];
  let lmc = 2;
  let lastLmc = 0;
  arr.sort((a, b)=>(a-b));
  for(let i=arr[0]; i<=arr[1]; i++){
    allNumArr.push(i);
  }
  for(let num of allNumArr){
    if(num == 1){
      console.log("met one so continue")
      continue
    }
    while(num % lmc !== 0 && (num+1) % lmc !== 0){
      lmc++;
      console.log("lmc Changes to "+lmc)
      if (lmc >= 1000000){
        break;
      }
    }
    if(num == allNumArr[allNumArr.length]){
      console.log("met last so broke")
      break;
    }
    let lastLmc = lmc;
    console.log(num+" lmc is "+ lmc)
  }
  return console.log("result is "+lmc);
}

smallestCommons([1, 5]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0

Challenge Information:

Implement a Range-Based LCM Calculator - Implement a Range-Based LCM Calculator

Could you explain how this should work? I’m not sure I understand that.

If num % lmc !== 0 is evaluated to falsy value, then second condition is not even checked.

Inside for…loop of each num, i want (while the condition is true, while (num/lcm) isn’t zero) every possibility of division of lcm to occur up until it find a number for lcm that is divisible by the current num of allNum and the number after it (+1) too.

Then lcm for num1 and num2 is valid and saved

then i want it to go over again for num2 and num3 (save the new bigger lcm), etc…

Hope I made myself clear ^^

for(let num of allNumArr){
    if(num == 1){
      console.log("met one so continue")
      continue
    }
    while(num % lmc !== 0 && (num+1) % lmc !== 0){
      lmc++;
      console.log("lmc Changes to "+lmc)
      if (lmc >= 1000000){
        break;
      }
    }
    if(num == allNumArr[allNumArr.length]){
      console.log("met last so broke")
      break;
    }
    let lastLmc = lmc;
    console.log(num+" lmc is "+ lmc)
  }
  return console.log("result is "+lmc);
}

I went ahead and combined your posts for you. In the future, just reply to the original thread to add further updates.

Thanks.