loading...

re: Daily Challenge #152 - Strongest Number in an Interval VIEW POST

FULL DISCUSSION
 

Not the most efficient answer, but it is!


const findStrongestNumber = () => {
 let interval = [
   parseInt(document.querySelector("#num1").value),
   parseInt(document.querySelector("#num2").value)
 ];

  let numStrength = [0, 0];

  for(let i = 0; i < interval.length; i++) {
    let num = interval[i];

    while ((num % 2) !== 1) {
      num/=2;
      numStrength[i]++;
    }
  }

  let answer = 0;
  if(numStrength[0] > numStrength[1]) {
    answer = interval[0];
  } else if (numStrength[1] > numStrength[0]) {
    answer = interval[1]
  } else {
    if(interval[0] < interval[1]) {
      answer = interval[0];
    } else {
      answer = interval[1];
    }
  }

  document.querySelector("#answer").innerHTML = answer;
}

CodePen

Code of Conduct Report abuse