Introduction to 'While' Loops in JS 11 Review


#1

I tried to combine both for and do/while loops in 1 function...

TEST 1
var hi = 0
var hello = function () {
for (var i = 0; i < 5; i++) {
do { console.log("let's see how many times it logs"); hi++
}
while(hi<3);
}
}
hello()

I got 7 logs.....
when i=0, log = 3 times (do once + hi = 1&2)
when i=1, log = 1 times (hi stay <3.....)
when i=2, log = 1 times
when i=3, log = 1 times
when i=4, log = 1 times

But in fact, I would like while i=1 to 4, hi will also count as 1 & 2 each time and there should be total 15 logs shown, so i do the test 2

TEST 2

var hello = function () {
for (var i = 0; i < 5; i++) {
do { console.log("let's see how many times it logs"); var hi = 1; hi++
}
while(hi<3);
}
}
hello()
.
But it hangs.......why?? and how to archive my purpose??


#2

To understand how to achieve this, we first must look at what is wrong with your code, test1:

var hi = 0
var hello = function () {
        for (var i = 0; i < 5; i++) { 
         do { console.log("let's see how many times it logs");  hi++
        } 
        while(hi<3);
            } 
}
hello()

The problem you have here, is that in the first run of the for loop, hi is set to 3. (make sense?), so in the second, third and so on run of the for loop, the do while loop gets executed once, then check the condition, which is true (hi is still 3), so the loop ends.

Now, in your test2, you set hi to 1 inside your do wihle loop, which means hi < 3 will always be false (it will increase to two, then in the next run, reset to 1, then be increased, so now it is 2 again, and so on, for an infinity. What you actually want to do, is reset hi before the do while loop:

var hello = function () {
        for (var i = 0; i < 5; i++) { 
         var hi = 0
         do { console.log("let's see how many times it logs");  hi++
        } 
        while(hi<3);
            } 
}
hello()

now, each time your for loop runs, hi gets set to 0, then increases till 3 (which results in 3 prints out) the for loop runs again, which reset hi, then your do while loop runs 3 times, resulting in 3 printouts, this repeats until your for loop stops


#3

I see. Thank you for your explanation, which is very clear...

it seems i have to regard do/while as a whole loop....but not separately.


#4

var king = 0;
var siva = function(){
while(king<4)
{
console.log("the king is siva");
king++;
}
};
siva();
when submit without the function siva the output is like
the king is siva
the king is siva
the king is siva
the king is siva
3
i want to know why the number 3 occurs and how its removed with the use of function


#5

It is the value of king, it occurs because of codecademy's console. How to remove? Make a local html file with internal or external js file, and run it. Now it shouldn't show in your browser console, you can read about the codecademy console here


#6

How is this done? The terms "local" and "internal/external" in this context are new to me.
If you'd like to link me to a relevant article instead of a potentially huge explanation, either would be appreciated.

Thanks


#7

local = on your computer. You can create .html, .css, .js and many more with a text-editor like notepad++, there are videos on youtube, js is very similar, just save it as *.js, and select all file types

JS is used on webpage (which are made in html), then you have two options: internal javascript, or external. This simply means in the html file (internal) or in a separate js file (external)