why is the inner loop in a question only working when it runs the first time?

I’m not sure what this is supposed to do, but I suspect

if (instances * 2 < (2 * 10 ^ 8))

is not doing what you want. ^ is bitwise XOR and has lower precedence than *, so (2 * 10 ^ 8) is 20 XOR 8 which is 28.

Use 2e8 to get 2 with 8 zeroes after it.

console.log(finalInstances(13, [40, 89, 79, 76, 66, 60, 8, 90, 19, 39, 53, 30, 93]))

function finalInstances(instances, averageUtil) {
  // Write your code here
  for (var i = 0; i < averageUtil.length; i++) {
    if (averageUtil[i] < 25) {
      if (instances > 1) {
        i += 9
        console.log(i)
        instances = Math.ceil((instances / 2));
      }
    } else if (averageUtil[i] > 60) {
      if (instances * 2 < 2e8) {
        instances = instances * 2;
        i = i + 9;
      }
    }
  }
  return instances
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top