Ranjan Raj Shrestha
Ranjan Raj Shrestha

Reputation: 99

Unexpected result inside a Nested Loop

let conditon = false

const test = [2, 0, 3, 4, 5, 6, 7]
for (let i = 0; i < 10; i++) {
  for (let j = 0; j < test.length; j++) {
    if (0 !== test[j]) {
      conditon = true;
       break;
    }else{
      conditon = false;
      break;
    }
  }
  console.log(conditon)
}

In this loop it console.log true but it should print false when it finds 0 in an array

Upvotes: 0

Views: 70

Answers (3)

Miu
Miu

Reputation: 844

There are already good answers here. Let me introduce this way as an extra answer.

const test = [2, 0, 3, 4, 5, 6, 7];
console.log(!test.includes(0));

const test2 = [2, 1, 3, 4, 5, 6, 7];
console.log(!test2.includes(0));

.includes()

array.includes(<value>)
  • .includes() returns true if a given value is in an array, false if not.
  • !test.includes(0) returns true if 0 is NOT in test. false if 0 IS in test.

Upvotes: 0

Cat
Cat

Reputation: 4226

Here's a simplified version, which you can examine to see how it works, and which you can use as a template for a more complicated version if desired.

let condition;
const nums = [2, 0, 3, 4, 5, 6, 7];

for(let i = 0; i < 2; i++){
  for (let num of nums) {
    condition = (num !== 0);
    console.log(num, condition);
  }
  console.log("\n--end of outer loop--\n\n");
}


Edit:
From your comment, I gleaned that after each trip through the outer loop, you want to report if any value in the array was zero. If this is what you're looking for, you'd do something like this:

    const nums = [2, 0, 3, 4, 5, 6, 7];

    for(let i = 0; i < 2; i++){
      let noZerosFound = true;
      console.log("checking: " + nums);
      for (let num of nums) {
        if(num === 0){
          noZerosFound = false;
          // Can include a `break` statement here for better performance
        }
      }
      console.log("noZerosFound: " + noZerosFound);
      console.log("\n");
    }


And JavaScript arrays also provide some useful built-in methods for this kind of situation. So if you want, you could simply do:

        const nums = [2, 0, 3, 4, 5, 6, 7];

        for(let i = 0; i < 2; i++){
          console.log("checking: " + nums);

          // Applies the function `(num) => num !== 0` to each element of `nums`. 
          //   If the result is true for every element, returns true.  
          //   Otherwise, returns false. 
          const noZerosFound = nums.every( (num) => num !== 0);

          console.log("noZerosFound: " + noZerosFound);
          console.log("\n");
        }

See the .every method and arrow functions on MDN for further explanation.

Upvotes: 0

olawrdhalpme
olawrdhalpme

Reputation: 190

You are continuously setting condition to true, because e.g. 0 !== 2 evaluates to true. This is the case for every element, except 0. 0 !== 0 which evaluates to false. You need to put an else check in there and set condition to false, then break out so that it doesn't continue and override your value again by setting condition back to true for the next iterations.

let condition = false;

const test = [2, 0, 3, 4, 5, 6, 7]
for (let i = 0; i < 10; i++) {
  for (let j = 0; j < test.length; j++) {
    if (0 !== test[j]) {
      conditon = true;
    } else {
      conditon = false;
      break;
    }
  }

  console.log(conditon)

  // Comment this part out if you want it to continue looping without immediately stopping.
  // Otherwise the loop ends once it hits 0.
  if(!condition)
    break;
}

This is not the best way to do this, mind you... I'm just providing you an example on why your code works the way it does.

Upvotes: 1

Related Questions