Intermediate Algorithm Scripting - Smallest Common Multiple

Hmm. Something is not working. It does not seem to be looping to the next i as I think it should.

  **Your code so far**
function smallestCommons(arr) {
let min=Math.min(...arr);
let max=Math.max(...arr);
let maxFact=1;
let flag=1;
let scm=0;
for (let j=min; j<=max; j++){
  maxFact*=j;
}
for (let i=max; i<=maxFact; i++){
  for (let k=max; k>=min; k--){
    flag=1;//SCM until it isn't
    if (i % k != 0){//if the k element of the range leaves a remainder for i
      flag=0;
      break;//Not SCM. should leave K with flag=0 until it gets to SCM
    }
  }//it should only leave k loop with flag=1 when it is at SCM
console.log(scm, i, flag);
  if (flag==1) {//if it has found an i without any remainders.
    let scm=i;
    break;
  }//otherwise next i.
}
return scm;
}

smallestCommons([1,5]);

P.S. I just revised this swapping in a == for a =

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Link to the challenge:

It seems to work if I move the return scm to above the break. The scm does not seem to have a value outside of the for loop.

???

Honestly, I am trying to figure out your logic, but somehow I can’t, though I solved it couple of times.

However,

I would take a look at this line for the start.

I got it to work. :upside_down_face:

I tried your code in fcc console. It doesn’t pass the last test.
I commented out your line console.log(scm, i, flag); of course. But this doesn’t change things I guess. So if this code passes the tests on your end, it’s super strange. Well , for me at least

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