Javascript Infinite For 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 3 years ago.
Improve this question
I am learning for loops in javascript and trying some things I wrote the following basic code.
This code generates an infinite loop that only prints the number 4 indefinitely in the console.
The problem is that i don't understand the logic behind this and the exact reason why it happens
for(let i = 0; i = 4; i++){
console.log(i);
}

In your loop, the condition part uses an assignment operator =, you should use a comparison operator such as !==. Note also that the condition should be false in order to exit the for loop. So !== it is (or <=, which I dislike for personal reasons), instead of ===.
for(let i = 0; i !== 4; i++){
console.log(i);
}
So your code runs indefinitely because i = 4 is truthy. And it prints 4 because i = 4 assigns 4 to i after increment

i = 4 is an expression which would return 4 and 4 is trucy value so the condition never gets false and the loop never ends
console.log(i=4); //4
console.log(Boolean(i=4)); //true
You should use less than operator
for(let i = 0; i <= 4; i++){
console.log(i);
}

Related

JavaScript For loop ignored [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
Im trying to make a little program in js that overturns the text you typed in. The code runs fine, but the for loop gets completely ignored, it doesnt do even one block of code from inside of it.
document.getElementById("StartButtonText").addEventListener("click",function(){
var text_value = document.getElementById('TextValue').value;
console.log(text_value);
var text_length = text_value.length;
console.log(text_length);
var final_text;
var order;
for (order = 1; order >= text_length; order ++) {
final_text[order] = text_value[text_length - order + 1];
console.log('for loop log ',order);
final_text = final_text + final_text[poradie];
}
console.log('after loop log ',order);
document.getElementById("TextTurningResult").innerHTML = final_text;
console.log(final_text);
});
Any ideas why it doesnt run?
Your loop will run as long as order is greater or equals than text_length.
I guess you wanted to write:
for (order = 0; order < text_length; order ++) { ... }
You should change the condition in your for loop to
for(order = 1; order <= text_length; order++){
...
}
In your case, the condition is false at the first iteration only.

Why does a for loop behave oddly in a JavaScript constructor? [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 new to constructors - I'm trying to use one to create a customizable object, with this sort of code:
class test{
constructor(range) {
var start;
if(range==="a"){
start = 56;
}
else if(range==="b"){
start = 53;
}
for(var i=start; i<(start+5); i++); {
console.log(i);
//construct an array here
}
}
}
const myTest = new test("a");
But only the last loop seems to execute!
The log shows just the value 61.
You have an semicolon to early. The result is an empty statement and an additional block statement outside of the loop.
Finally you get the last value of i.
for (var i = start; i < (start + 5); i++); {
// ^

access property in node.js [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 6 years ago.
Improve this question
sorted = Object.keys(PLAYER_LIST).sort(function(a,b){return a.score - b.score}); // have key value
console.log(PLAYER_LIST[sorted[0]].team); // result:"A"
for(var loop=0; loop<=sorted.length; loop++) {
if(PLAYER_LIST[sorted[loop]].team == "A") { // error
some code...
}
}
When I sorting like this. console.log working well. But if sentence is not working. I receive error message. I don't know why. please help me.
if(PLAYER_LIST[sorted[loop]].team == "A") {
^
TypeError: Cannot set property 'team' of undefined
Your loop goes one step too far
for (var loop = 0; loop <= sorted.length; loop++) {
// ^^ here
An arrays length starts at zero when it's empty, and is 1 if the array contains one item.
If you have an array
var array = ['a']
arrays are zero based, so the first and only item is array[0], and the length is 1
When you iterate and you go all the way to the arrays length, you go one index too much, and you end up trying to get array[1], which doesn't exist.
What you wanted was
for (var loop = 0; loop < sorted.length; loop++) {...

Understanding the assignment operator - javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In javascript my understanding of the assignment operator is that it is the = sign.
var x = 120
but if you then wanted to do a for loop using this variable, you would normally shorten it for example:
for (x = 120; x < 140; x++);
so in between the paranthesis,it appears that the < symbol is also an assignment operator, as it states it is less than 140 and should be increased to 140.
Could someone explain more clarity on this or point me in the right direction, as most things i find shows this rightfully as the less than operator.
Your function myfunction (i=1; i < thisVar; i++) is a syntax error. We can't explain how that code works because it doesn't.
You may be thinking of the for loop:
for (i=1; i < thisVar; i++) {
}
The for loop has three expressions within its () that are separated with ;:
An initialization (i=1 in your case) that occurs at the very beginning, before the first test (see #2)
A test (i < thisVar in your case) that is performed prior to each iteration of the loop and determines whether the loop ends
An update (i++ in your case) that occurs after each loop iteration, before the test
This is intrinsic to how for loops work, and is not general-purpose; you can't just do that within () anywhere you like, it has to be on a for loop.
The < in that, as you can see above, is part of the test — a condition that must be true for the loop to continue. It's not an assignment. It's a relational operator comparing i with thisVar to determine whether i is less than thisVar.

In javascript, can I use the modulus operator (%) on variables instead of integers? [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 6 years ago.
Improve this question
I'm working on a Euler problem and am trying to build a function that checks a number to see if its a prime. I get error messages about the line:
if (a)%(b)==0{
Is my syntax wrong or is it impossible to use % on a variable rather on an integer?
var x = Math.sqrt(600851475143);
var y = Math.round(x);
y++;
console.log(y);
//find all of the prime numbers up to the square root number. Put them in an array.
//Check each ascending number against the prime numbers in the array to see if %=0
var primes = [2,3];
var a =(3);
while (a<y){
a++;
isPrime(a)
}
function isPrime(arr){
for (var i = 0; i < arr.length; i++){
var b = primes[i];
//next line is a problem
if (a)%(b)==0{
break
}else{
primes.push(a);
}
}
}
You can always use operations on variables. When the script is run, the variables are substituted with the real values associated with the variables.
var a = 3,
b = 5;
if(a%b == 0) {}
Is equal to
if(3%5 == 0) {}
You just used the wrong syntax in your statement:
if (a)%(b)==0 {}
It should be:
if(a%b == 0) {}
In JavaScript, you need to wrap your if statement with squiggly brackets, not the variables. Your code would trigger a syntax error because the if statement is written incorrectly and it doesn't expect a random modulus, equal signs and other symbols outside the parentheses.
Yes you can use it
But the issue is here in if loop
if (a)%(b)==0{ // Here it is assuming the the condition statement ends with )
which is after a
In reality it will be
if ((a)%(b)==0){ // Note braces pointed by ^^
^ ^
break
}else{
primes.push(a);
}
}
}

Categories

Resources