Why do I have an infinite loop? [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm learning javascript and I don't understand why this code is creating an infinite loop.
for (var i = 8 ; i < 120; i+12) {
console.log(i * 1);
}
If I make an assignment of i like i = i + 12 then everything works like it should. Please explain what is going on here.

You'll learn that JavaScript does some wonky and questionable things...
I've never seen a use-case with the parameters you've set with a for loop, but I'm assuming you know the defaults.
Anyway, what you are doing is not increment i by 12, so it will continue to loop, given i never actually increases.
The operator += should do the trick, as it will take the previous value of i, and add 12 to it.

You need i += 12 to do assignment instead of just i + 12 (which is a plain expression).

because i+12 does not increment and store the incremented value in i. So the value of i is always 8. Wheras i=i+12 increments and stores the value in i and the loop terminates when i >= 120.

you need to use i+=12 Check out the operators of JavaScript
for (var i = 8 ; i < 120; i +=12) {
console.log(i * 1);
}

Related

Number prediction from 1 to 10 from javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is my code but when i run this code in console always giving me Congratulations.
Help me for solve this problem.
var val=Math.floor(Math.random() * 10) + 1;
console.log(val);
var Predict = Number(prompt("Prediction ?"));
for(var i=1 ; i <= 3; i++){
if(Predict<val){console.log("Up")};
if(Predict=val){console.log("Congratulations") };
if(Predict>val){console.log("Down")}
}
Equal operator assigns the right hand to the left hand and so the result is always true! To compare two values use double equals like this:
if (Predict==val){console.log("Congratulations") };

Issues with displaying certain array elements with a conditional in javascript [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to use a for loop to display all the elements in my array that are greater than 5. I am using an if-statement to determine the values greater than five, but when I run the code, I get values printed to the console that are both below and over five. I have tried creating variables to store the index values and tried using the && operator, but none of these have worked.
Here is the code for reference:
var myArray = [];
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
appendItem(myArray, randomNumber(1,10));
console.log("Original: " + myArray);
console.log("Values greater than 5");
// the part I have issues with
for (var i = 0; i < myArray.length; i++) {
if (myArray[i] > 5) {
console.log(i);
}
}
You are printing i not the array element.
try
console.log(myArray[i]);

When using a while loop in JS, why is it possible to return a value that makes the condition false [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Depending on where I place the statements inside the while loop, it may or may not return a value that makes the condition false
let n = 0;
while (n < 3) {
n++;
console.log(n); // Will log 1, 2, and 3
}
Whereas if it were written
let n = 0;
while (n < 3) {
console.log(n);
n++; // Now the program logs 0, 1, and 2
}
Shouldn't both cases only log 1 and 2 (and 0 for the second one) since the termination point for the loop is once it reaches 3?
How does this syntax make a difference?
Computers don't know loops. They also don't notice when a value in memory changes.
They can only do some very basic math, store things, and do conditional jumps. Your while loop basically looks like this internally (pseudocode):
if n smaller 3 jump to end. // conditional jump
increase n.
log n.
jump to first line.
So it really doesn't matter when n changes. The loop goes on till the condition is reached again, if the condition is false then, it'll exit there (you can break; [jump] out of loops though).

why does for loop not run what is inside? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I've written an extremely simple for loop in JS and the loop just doesn't run. My code is
for (i = 0; i >= 100; i++) {
console.log('boop');
};
and the loop doesn't even do anything. Does anybody know why the code just gave up?
Your start value does not match the condition, you need to check for smaller than the maximum value.
for (i = 0; i <= 100; i++) {
console.log(i, 'boop');
}
Because i is never greater than or equal to (>=) 100. Change the sign to <= and the loop will run.
The condition of the loop (i >= 100) isn't true even for the first iteration. The code within the loop will only execute while the condition is true.
Just remember that for is kind of a while..do loop
It validates the condition first, so, a for kind of says for (this variable, under this condition, that changes after each iteration this way) do
So, your condition is false from the beginning, it will never do anything.

Javascript "For loops" difference between i-5 and i=-5 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I want a loop that starts with i=100, and decrements by 5 on each iteration. However, this produces an error:
for(var i=100;i>=1;i-5)
{
document.write(i+"<br />");
}
But 'i=i-5' works:
for(var i=100;i>=1;i=i-5)
{
document.write(i+"<br />");
}
Why?
The question is how we can assign a variable to a variable i=i-5.I go the question answered I taught "i=i-5" was a expression the value of i variable is i-5 and no calculation happen its just a stable variable.
The answer is that it is taking a the i value and subtracting it by 5 and not assigning.
If you just write i - 5, there is no left-hand variable, which means there is nothing which is taking the value of i and subtracting it by 5. If you write i = i - 5 then you will decrement 5 since you now have a left-hand variable.

Categories