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.
Related
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") };
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]);
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).
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 6 years ago.
Improve this question
I'm creating a site and i needed to do some js. I'm not that good with it but tought i would figer it out. Not. I created a for loop but it does not run.
function order(user,product){
var index;
for(var i = 0; i<users.lenght; i++){
if(user == users[i]){
index = i;
break;
}
}
var budget = budgets[index];
alert(budget);
}
the creation of the users and budgets arrays are done with php and after checking with alert() it was how it should be.
Can anyone help me please?
lenght is spelt length. The misspelt property does not exist, so it undefined, which is equivalent to 0.
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);
}