Introduction to while loop, 5th exercise(practice makes perfect)


#1

i m kinda stuck in it, could any post the whole solution so that i can see


#2

Better show what you already got or explain what is unclear.


#3

@haxor789 completed, srry to bother later it worked


#4

I don't know where to put the "count++"


#5

@eightpeaks after console.log...

after what's in console print out at console , then count++ will happen and increase count by 1 so that condition continues.


#6

Important is that it is inside the loop because you want to change the value of count so that the condition relying on count get's false after 3 runs. Where exactly doens't matter that much, I even would place it before the console.log as otherwise you might see a number in the console because the last value is echoed (not harmful but confuses some folks) but as said important is only that it is part of the loop. It would only be important if you use count inside your console.log but as you don't do this it doesn't matter.


#7

can you please send the solution? I am blocked with it, thanks


#8

//Remember to set your condition outside the loop!
var count=0

var loop = function(){
while(count <3){
console.log("I'm looping!");
count++
}
};

loop();


#9

I have copied paste it (I had already did the same) but I get always the same error:

Oops, try again. Hey now--only one console.log() call!

Thanks a lot again


#10

@fdn_dimatteo which solution is u taking about


#11

@smondaca u didn't pass the arguments in loop,

Like loop(count)
function (count)

var count = 0;
var loop = function(count){
while(count<3){
//Your code goes here!
console.log( "I'm looping!");
count++;
}
};

loop(count);


#12

@fdn_dimatteo

var count = 0;
var loop = function(count){
while(count<3){
//Your code goes here!
console.log( "I'm looping!");
count++;
}
};

loop(count);

Check ur code must be a syntax prblm


#13

@ankurlvv You're right. That's odd. I didn't even look back at that. What I had posted above gave me the green light to keep working. Weird that it allowed me to continue.


#14

@ankurlvv and @smondaca Both of your codes print the message 3 times. @ankurlvv code is a bit better because it would also work if you run it twice

loop(count);
loop(count);

whereas @smondaca's code would only be able to produce the result once so

loop(); -> 3 outputs
loop(); -> no output

still it is not that wrong and unfortunately a pretty often used "solution".

Maybe the problem becomes more visible if we think about global and local variables. If we have a look at @smondaca's code:

var count=0
var loop = function(){
    while(count <3){
        console.log("I'm looping!");
        count++
    }
};

loop();

we can see that no parameters are used, so count is the global variable count from before the function. The problem is that you modify count inside of the function so after having run the function count will be 3. So if you'd run the function again it will skip the while loop because count is already be 3.

@ankurlvv solves this problem by using a parameter:

var count = 0;
var loop = function(count){
	while(count<3){
		//Your code goes here!
		console.log( "I'm looping!");
		count++;
	}
};

loop(count);

here count inside of the function is not the same count as outside of the function, they are in fact 2 seperate variables with the same name that are only connected by the value you pass to the function. Maybe it get's clearer by changing the name of the parameter:

var count = 0;
var loop = function(innerCount){
	while(innerCount<3){
		//Your code goes here!
		console.log( "I'm looping!");
		innerCount++;
	}
};

loop(count);

Now the inner count is just 0 because you assing 0 to it by using the parameter count. As the value of count is not changed in the process because you used an entirely different variable, you can do the same thing again.

Last but not least a version of @smondaca's code without parameter that would do this as well:

var loop = function(){
    var count = 0;	
    while(count<3){
		//Your code goes here!
		console.log( "I'm looping!");
		count++;
	}
};

loop();

Hope it helps!


#15

Introduction to while loop, 5th exercise(practice makes perfect)
I am stuck in it.
Can you please post me the whole solution?


#16

No. Please open your own thread, post your code and mention the error message and we can help you. But just posting the code is not considered this helpful here and giving a good explanation without knowing the problem is barely possible. So as said open a question and provide the information and you might get a better answer.


#17

sorry your code did not help me it just said error "line 1 " thank you for your help tho guys


#18

The best thing would be if you could make a new thread with your code, the exercise name and number and maybe some further informations like the error message. Without the information about the content of "line 1" it is simply impossible to tell you what the problem is.


#19

@kara_101 yes.. mr.@haxor789 i spot on. Pls mention ur code in thread


#20

@chopra_sanjeev2008re pls open ur own thread and post code, at that point we will not only help ur better. You might learn something new as well