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.
Related
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);
}
i wanted see what the output will be if the condition for executing the code block is i = num. when i run the code, vs code terminal just shows node (path of my file) and i subsequently cannot execute any code from any file again. i must restart vs code and change the condition to i < num. why is this so? running the code in the code snippet here also seemingly crashes the page.
is this an infinite loop? however i don't see how it is an infinite loop as the condition to run the code is i=num. if i =/= num, shouldn't an error be returned instead of crashing vs code?
function FirstFactorial(num) {
let solution = 1
for (let i = 1; i = num; i++){
solution *= i
}
return solution;
}
console.log(FirstFactorial(5))
One thing to address. The second parameter for the for loop should be a condition.
i = num
is not a condition.
If you want to compare those variables you can use the triple ===
the issue is the i = num in your:
for (let i = 1; i = num; i++){
you want i <= num for the for loop to iterate.
for (let i = 1; i <= num; i++){
solution *= i
}
The for loop iterates while the second block i <= num evaluates to
true.
As soon as its false it will break out of the for loop,
as 1 != 5 (where num is 5 in your example) having i = num; as your second block evaluates to false right away and the for statement does not run.
i searched around for a couple of questions related to the use of the for loop and the setInterval function in JavaScript but i couldn´t find a concrete answer on why this snippet doesn´t work. Could someone explain please what´s happening under the hood and why this code doesn´t print anything at all?
for (let i = 0; i++; i < 10) {
window.setInterval(function () {
console.log('Test');
} , 100)
}
Your for loop is not correct. The condition needs to be the second statement in the for loop.
Following code should work.
for (let i = 0; i < 10 ; i++; ) {
window.setInterval(function () {
console.log('Test');
} , 100)
}
Expected Syntax for loop. You can read more here
for ([initialization]; [condition]; [final-expression])
statement
EDIT 1:
Though all answers (including mine) mentioned that condition needs to be second statement in the for loop which is correct. There is one more additional important behavior.
The for loop for (let i = 0; i++; i < 10) is actually correct in terms of grammar and even the javascript runtime executes this code.
But, as in your case, if the condition is evaluating to falsy value then it would exit the loop.
Breaking your for loop for (let i = 0; i++; i < 10) into each seperate construct
Initialization: let i = 0; This statement initializes the value of variable i to 0.
Condition: i++; Javascript evaluates the statement to check if the statement is true or not. In case of i++ the runtime firstly checks for the current value of i which is 0 . Since 0 is considered a falsy value the condition evaluates to false and hence the statement is not executed. Also, i++ statement is a post increment which basically increments i and then result the original value of i.
So, if you would have written loop like below, using the intiliaztion of i=1, then it would have worked, though it would be running infinitely untill the browser/Server crashes as the condition i++ would always evaluate to true. I hope that makes sense.
for (let i = 1; i++; i < 10) {
// Statements would run
}
Or
for (let i = 0; ++i; i < 10) { **// Pre increment**
// Statements would run
}
Or
for (let i = 0; i=i+1; i < 10) { **// increment i by assigment
// Statements would run
}
Douglas Crockford in his book Good Parts mention about the usage of ++ & -- and how it can confuse readers.
your for loop syntax is wrong, should be
for (let i = 0; i < 10; i++)
your setInterval code will run every 100 milliseconds for each iteration of the loop (so 10 times every 100 milliseconds)
Nothing to do with setInterval, you simply malformed your for loop:
This:
for (let i = 0; i++; i < 10)
Should be this:
for (let i = 0; i < 10; i++)
First declare the initial state of the loop, then the terminating state of the loop, then the incremental change of the loop.
Observe.
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.
I'm kinda new to Javascript and currently going over the book Professional Javascript for Web Developers and I came across this code which uses a break statement to exit the current loop and jump to a label named outermost.
Now I understand what break and labels do but I can't wrap my head around why the value ends up being 55 at the end?
Ok so the for loop with var i will loop 4 times then at 5 it breaks out to label:outermost and same with j so the first iteration i = 4 and j = 4 and num = 2. I guess this part confuses me.. at what point does the code stop. My first instinct if I were to code this from scratch is to have an outside variable and set the condition on that. But with the below code I don't get where the control structure lies and the final value. Appreciate any help or to be pointed in the right direction, thanks.
var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
num++;
}
}
alert(num);
This nested loop is emulating an odometer. i is the 10's digit, j is the 1's digit. Every time the 1's digit changes, num is incremented; at the start of each iteration, num contains the odometer's value.
The loop stops when both i and j are 5. At that point, the odometer would read 55, and that's what is in num.
When i was 0 to 4, the innermost loop is executed 50 times. When i = 5, the innermost loop is executed just 5 times until it reached i==5 && j==5 and jumped out. So it's total of 55 times.