Extra output from Javascript nested loop - javascript

while practicing loop in JS, I wrote following code.
var bool = true;
while(bool) {
for (var i = 3; i >= 0; i--)
{
console.log(i);
bool = i;
}
}
The output I expected was:
3
2
1
0
(1 digit per line)
The output I encounter was:
3
2
1
0
0
(1 digit per line)
My question is - how does the code or environment component produce the extra "0"?
Thank you for your time and help.
The observed result is produced in Chrome (F12 -> console tab)
screen shoot from chrome
Also, on code academy's practice setting.
And somehow I cannot produce /or observe any result from the "Run code snippet".
UPDATE:
By switching
console.log(i);
and
bool = i;
I got 3 2 1 0 instead.
This would confirm Pointy's answer - no expression, only function call - Thanks again!

The last 0 is just the console mechanism telling you the value of the last expression statement evaluated. If you change it:
var bool = true;
while(bool) {
for (var i = 3; i >= 0; i--)
{
console.log(i);
bool = i;
}
}
"hello world";
you'll see hello world instead of the last 0.

Related

Don't understand why my for loop code isn't working

I was playing with the break and continue keyword but I notice in chrome console this code doesn't work
for (let i = 0; i > 5; i++) {
console.log(i);
} //this will print all number from 1 to 5 but console showing undefined
However I tried changing i = 5 or i === 5 still it doesn't work
But when I changed it to i < 5 then it yields correct result.
Why is that?
your condition is i > 5. At the beginning i = 0 so it will not run ever.
put i < 5
Please check following diagram
Condition getting false on the first time so code block won't execute
i<5 indicates that the for loop will run as long as i is less than 5. i>5 indicates that 0 is less than 5 (let i = 0). So the correct program should be -
for (let i = 0; i <= 5; i++) {
console.log(i);
}

JS square from 1 - 10

Hi I need to add to the table square fr 1 to 10 and I don't know what's wrong with my code (I'm new to JS)
Thanks for help
var kwad =[];
a = 1;
for (a>0; 10 === a; a++){
kwad[a] = Math.pow(a,2) ;
}
In JavaScript for loop, each iteration will happen as long as second condition evaluates to true. In your case that never happens, because 10 === a always equals false (because a equals 1, so 1 === 10 will give you false). You should fix your code in following way:
const kwad =[];
for (let a = 0; a < 10; a++){
kwad[a] = Math.pow(a,2) ;
}
Ps. Besides all, first statement in for loop is initialization, so code a > 0 doesn't really makes sense.

I'm having trouble with this exercise on hacker rank please look

https://www.hackerrank.com/contests/projecteuler/challenges/euler001
Here is the problem I'm confused what the parseInt readline statement
and also the the var n statement mainly..
when i run my code it seems to count up to ten twice probably a simple problem just not seeing it and was hoping I could get it explained so I can keep working on project euler problems
Thanks
function main() {
var t = parseInt(readLine());
var sum = 0;
var arr = [];
for(var a0 = 0; a0 < t; a0++){
var n = parseInt(readLine());
for (var i = 0; i < n; i++)
if (i % 3 === 0 || i % 5 === 0){
arr.push(i);
sum += i;
};
console.log(arr);
};
}
Maybe I'm not following exactly what is your question.
The parseInt is a javascript function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
The readLine() function is defined for you, it will give you "the next line" that was captured from standard in.
All (or most) of the hackerrank problems offer the input for the problem thru standard-in and expect the result from standard-out. So for this problem, hackerrank has created this boilerplate code for reading that input.
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
There is filling the input_stdin_array array that is used on the readLine() function.
And about the
when i run my code it seems to count up to ten twice
The problem mentions:
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
So you are printing the array T times (for the default test case is 2), so that why you probably see the "up to ten 2 times"
I hope this helped, and probably you could start with a couple of the https://www.hackerrank.com/domains/tutorials/30-days-of-code challenge so you get a better grasp of how to work on the problems.
Regards
Declare the array after the first for loop. You are using the same array for every test case, even though it still contains numbers from the previous test cases. Same for the sum.
for(var a0 = 0; a0 < t; a0++) {
var arr = [];
var sum = 0;

Strange behaviour of for loops

Can anyone tell me why for loop increments even on failed iteration?
for (var n = 0; n <3; n++) {
alert(n); // displays 0 , 1 , 2
}
alert(n); // gives 3
But shouldn't it be like
if(condition):
//desired stuff
increment;
else:
exit;
I seldom use iteration variable mostly I just throw them away upon loop completion but in this case found it to be the cause of a bug
Conceptually n++ is called just after the final statement of the loop body, and the stopping condition is evaluated just before the first statement of the loop body.
So your code is equivalent to
for (var n = 0; n < 3; ) {
alert(n);
n++;
}
Viewed this way, the reason why n is 3 once the loop exists ought to be obvious.
Note that in javascript, n leaks out of the for loop.
for (var n = 0; n <3; n++) {
alert(n);
}
alert(n);
Working of for loop is as follows -
First it initialize the n to 0;
Then it checks the condition whether it is true or not in this case condition is n<3.
Finally it increments the n and again check the condition and if it is true,It again goes in for block. And if the condition is false, It exit the for loop.
In your code when n=3 condition get false. So final value of n is 3.
Just executed the code at chrome console:
It works as expected.
As you have commented that n will be alert with 3, it is wrong, because when n will be 2, condition will be checked i.e. 2<2 will be wrong, then it will be jump from the loop and will alert 2 not 3.

Basic While loop query - why is "1" logged?

Why does this while loop print "1" at the end?..I only want it to print console.log statement. Saw when using Codecademy.
for (i = 0; i < 2; i++) {
console.log("I understand for loops twice..lol");
};
var whileUnderstand = 0;
while(whileUnderstand < 2) {
console.log("I understand while loops twice..lol");
whileUnderstand++;
}
That question doesn't have the direct answers to the question that i asked. Furthermore, it only includes console.log statements instead of loops. Mainly, there are no answers saying that "The console is simply outputting the last evaluated value of the statement." which is the answer that solved my question.
This will only happen when running the code in a browser console.
It's caused by this line:
whileUnderstand++;
The console is simply outputting the last evaluated value of the statement.
The reason only 1 is logged and not also 0 is that outside of a console.log() call, only the last statement is logged.
For example if I have the following code snippet only "d" is logged:
var a = "a";
var b = "b";
a = "c";
b = "d";
var whileUnderstand= 0;
while(whileUnderstand<2) {
console.log("I understand while loops twice..lol");
whileUnderstand++;
}
Because it doesn't increment whileUnderstand when its equal to 2 if you want to print 2, you need to do this
var whileUnderstand= 0;
while(whileUnderstand<3) {
console.log("I understand while loops twice..lol");
whileUnderstand++;
}

Categories

Resources