Home > OS >  Don't understand why while runs itself again after ignoring a command
Don't understand why while runs itself again after ignoring a command

Time:10-15

I have this code in JS

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  var tank;
  var count;
  var johnny = true;
  for (var j = 0; j < A.length; j  ) {
    count = 0;
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log(i);
      console.log(A.length);
      console.log(count);
      console.log(johnny);
      tank  = A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        johnny = false;
      }
      count  ;
      i  ;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));

What I don't understand is when I run the program, at first count = 0, but when I enter the while loop for the first time, instead of running the command count and changing its value to 1, it doesn't run it, keeps count at 0 and then changes it after re-entering the loop.

TLDR: While loop is running twice with one of the variables staying with the same value instead of changing it as one of the lines in the loop suggests.

CodePudding user response:

It is not clear what are you trying to do but the condition of the while loop will not be always true which makes count= 0 inside the for loop. So, you need to put var count =0;

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  
  var tank;
  var count=0;
  var johnny = true;
  for (var j = 0; j < A.length; j  ) {
   
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log(i);
      console.log(A.length);
      console.log(count);
      console.log(johnny);
      tank  = A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        johnny = false;
      }
      count  ;
      i  ;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));

CodePudding user response:

TLDR: While loop is running twice with one of the variables staying with the same value instead of changing it as one of the lines in the loop suggests.

You're assuming that count never got incremented, because you're assuming the while loop continues a second time around. In reality, the while loop exits after a single iteration, and the for loop resets count to 0. Then the while loop is entered again, but with a different value for j.

Details

The first time through your while loop, B[i] is 2 and A[i] is 1. tank = A[i] makes tank have a value of 1. Therefore, b[i] <= tank is false, so you set johnny to false. That means at the end of that iteration, your while condition (... && johnny == true) is false, so you don't continue through the while loop a second time.

Instead, control moves out to the for loop, which resets count to 0.

To debug issues like this in the future, it helps to learn to use the debugger: you can place a breakpoint where you want, and see how the control flows and how the variables change as you step through it. If you find that's not possible, you may need to be even more verbose with your logging, as below.

//param A : array of integers
//param B : array of integers
//return an integer
function doesitwork(A, B) {
  if (A.length != B.length) {
    return -1;
  }
  var i = 0;
  var tank;
  var count;
  var johnny = true;
  for (var j = 0; j < A.length; j  ) {
    console.log("entering for loop");
    count = 0;
    tank = 0;
    i = j;
    johnny = true;
    while (count < A.length && johnny == true) {
      console.log("entering while loop");
      console.log("i", i);
      console.log("j", j);
      console.log("A.length", A.length);
      console.log("count", count);
      console.log("johnny", johnny);
      tank  = A[i];
      if (B[i] <= tank)
        tank -= B[i];
      else {
        console.log(B[i], tank, "setting johnny to false");
        johnny = false;
      }
      count  ;
      i  ;
      if (i == A.length)
        i == 0;

    }
    while (johnny == true) {
      if (count == A.length)
        return j;
    }
  }
  return -1;
}

var A = [1, 2];
var B = [2, 1];
console.log(doesitwork(A, B));

  • Related