Reviewing while loops in JS


#1


Goodday coders,

Reviewing while loops in JS...

Can anybody please clarify to me why from below code my "whileloop" string is not being printed?


var i=0

var forloop=function(){
    for(i=0; i<5; i++){
        console.log("forloop");
    }
};
var whileloop=function(){
    while(i<5){
        console.log("whileloop");
        i++;
    }
};
var doloop=function(){
do{
    console.log("doloop", "doloop", "doloop", "doloop", "doloop");
}
while(i===1);
}

forloop();
whileloop();
doloop();

Thanks!


#2

I modified your functions with some console.log debug statements. They are as follows

var i=0

var forloop=function(){
    console.log("var i before forloop begins:" + i);
    for(i=0; i<5; i++){
        console.log("var i during forloop: " + i);
        console.log("forloop");
    }
    console.log("var i after forloop begins:" + i);
};
var whileloop=function(){
    console.log("var i before whileloop begins: " + i);
    var i=0;
    while(i<5){
        console.log("var i during whileloop begins: " + i);
        console.log("whileloop");
        i++;
    }
    console.log("var i after whileloop begins:" + i);
};

The logs I received from doing this were:

/*
var i before forloop begins:0
var i during forloop: 0
forloop
var i during forloop: 1
forloop
var i during forloop: 2
forloop
var i during forloop: 3
forloop
var i during forloop: 4
forloop
var i after forloop begins:5
var i before whileloop begins: undefined
var i during whileloop begins: 0
whileloop
var i during whileloop begins: 1
whileloop
var i during whileloop begins: 2
whileloop
var i during whileloop begins: 3
whileloop
var i during whileloop begins: 4
whileloop
var i after whileloop begins:5
*/

The problem seems to be that you are not redeclaring the i variable in your functions.

I understand what you were thinking declaring the global i variable at the top. I wrote some test functions to confirm whether you were being wronged or not. They are as follows, along with their results:

var test = 0;

function adder() {
  console.log("var test before: " + test);
  test++;
  console.log("var test after: " + test);
};

/*
tester();
var test before: 0
var test after: 1
undefined
tester();
var test before: 1
var test after: 2
undefined
tester();
var test before: 2
var test after: 3
undefined
tester();
var test before: 3
var test after: 4
*/

function looper() {
  console.log("var test before loop: " + test);
  for (test = 0; test < 10; test++) {
     console.log("var test during loop: " + test);
  }
  console.log("var test after loop: " + test);
};

/*
looper();
var test before loop: 4
var test during loop: 0
var test during loop: 1
var test during loop: 2
var test during loop: 3
var test during loop: 4
var test during loop: 5
var test during loop: 6
var test during loop: 7
var test during loop: 8
var test during loop: 9
var test after loop: 10
*/

function whiler() {
  console.log('var test before while loop: ' + test);
  test = 0;
  while (test<10) {
    console.log('var test during while loop: ' + test);
    test++;
  };
  console.log('var test after while loop: ' + test);
}

/*
whiler();
var test before while loop: 10
var test during while loop: 0
var test during while loop: 1
var test during while loop: 2
var test during while loop: 3
var test during while loop: 4
var test during while loop: 5
var test during while loop: 6
var test during while loop: 7
var test during while loop: 8
var test during while loop: 9
var test after while loop: 10
*/

You can see how this example of mine, with console log results from the browser console confirm that the globally declared test is being grabbed by these functions & manipulated.

I would guess this is an issue with codecademy parser. Either way it's better to build your iterator variables into your loops than to use a global one, unless you know what you're doing and you need a single iterator.


#3

I see.
Thanks for the insight.
I'll keep this test method in mind for next time.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.