vendredi 6 mars 2015

Result of IF statement nested in WHILE loop confusing me

Here's my full code:



var products = ["Choo Choo Chocolate", "Icy Mint", "Cake Batter", "Bubblegum"];
var hasBubbleGum = [false, false, false, true];
var i = 0;

while (i < hasBubbleGum.length) {
if (hasBubbleGum[i]) {
console.log(products[i] + " contains bubblegum");


This code returns to the console:



Bubblegum contains bubblegum


What confuses me is this IF statement nested in my while loop. I know this code is correct but I don't understand why it's bypassing the IF statement when hasBubbleGum is set to FALSE.


My original thought was that printing to the console would yield:



Choo Choo Chocolate has bubblegum
Icy Mint has bubblegum
Cake Batter has bubblegum
Bubblegum has bubblegum


Am I missing a key fundamental idea that IF statements ignore boolean values of FALSE?


Aucun commentaire:

Enregistrer un commentaire