Understanding the assignment operator - javascript [closed] - javascript

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.

Related

JS why FOR LOOP not iterating when condition is A&&B but works when B&&A (A==B==true) [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 2 years ago.
Improve this question
This is my first question so hello everyone!
I wanted to write condition in way to terminate loop ASAP.
Loop which doesn`t work but should terminate loop faster:
for (
let i = 0;
transactionsSortedByTime[i].unixTime <= unixEndTime && i < listLength;
i++
) {}
Loop which works:
for (
let i = 0;
i < listLength && transactionsSortedByTime[i].unixTime <= unixEndTime;
i++
) {}
Till now I was certain that results of A&&B and B&&A are the same but terminates with false result in different time (when one of them {A or B} is false).
My question is why second one loop iterates properly while first one doesn`t.
In your case, A & B is not same as B & A because in first code snippet, you are accessing the i'th index of transactionsSortedByTime array before checking if i is less then listLength. Doing this will give you undefined when i < listLength is false.
So i < listLength should be written before transactionsSortedByTime[i].unixTime <= unixEndTime to make sure your code doesn't tries to access an index that is out of bounds of the transactionsSortedByTime array.
Just add (), the loop structure apparently takes the bool value of the first expression, to solve this you must add parentheses, so that your expression takes the value of your && operator:
for (
let i = 0;
(transactionsSortedByTime[i].unixTime <= unixEndTime && i < listLength);
i++
) {}

Javascript Infinite For Loop [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
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);
}

JavaScript Infinite "for-loop" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
EDIT: Looks like I messed up by my method of posting. For future help to others who experience this problem, I will be cleaning up this question.
I've been learning about JavaScript recently and ran into an issue - I believe it's a bug, but I can't find anything about it or how to fix it. For some reason, I get stuck in an infinite for-loop, where I see a constant barrage of '0' instead of the expected '0 1 2'. This is my current setup:
for (var i = 0; i < 2; i = i++) {
console.log(i)
}
Any help would be much appreciated. For convenience, I took a video of it (20 seconds or so). [I realize that videos do not help those who are trying to help after doing some reading, but I think it could still help those who have the same issue as I did. Therefore I decided to keep the video link just for that reason.]
As extra info, I ran this through Windows 10 WSL (bash for windows), Ubuntu 16.04. I am using Visual Studio Code.
https://photos.app.goo.gl/aJEgY3hEJVTMwRmK6
Thanks ahead of time!
This behavior is expected:
Increment (++)
The increment operator increments (adds one to) its operand and returns a value.
If used postfix, with operator after operand (for example, x++), then it returns the value before incrementing.
If used prefix with operator before operand (for example, ++x), then it returns the value after incrementing.
The key being for i++ it returns the value before incrementing
The other key being that primitives in JS are immutable, so when you assign a number, a new number is created and held in that variable.
Therefore your loop is saying i = 0, then set i = 0, then the previous 0 is set to 1.
The usual way of doing this kind of loop is to just increment the value without reassignment:
for (var i = 0; i < 2; i++) {
console.log(i)
}
Remove the i =
Also, you may want to declare the variable i outside of the for, like this:
var i;
for (i = 0; i < 2; i++) {
console.log(i)
}
The reason I prefer to have the var in the for is that it is not creating a variable limited in scope to the for loop, it will still be at the same level.

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);
}
}
}

For loop and hoisting [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have a question.
For example I use for loop like this:
for ( var i = 0; i < some_length; i++ ) {
/* some code */
}
After that variable i is hoisted.
Does it mean that I always should declare i variable at the beginning of the scope?
var i;
for ( i = 0; i < some_length; i++ ) {
/* some code */
}
UPD:
I know that both loops work the same way.
I mean which one is more correct?
Modern JavaScript supports block scoping via let.
for ( let i = 0; i < some_length; i++ ) {
/* i is defined here */
}
/* i is not defined here * /
Back to the original quetion: which example is more correct?
I would argue that the second one is less error prone.
In your example - first one is classic type declaration of your iterator variable. In other cases like when you operate on many functions/objects/variables i recommend you to declare all variables on the beginning of your scope/object/function.
When you see code wrote in that way in future it will be much easier to see what is going on in here. You just look on first 10-15 lines of code and you would not search for every variable inside - everything will be explained in the beggining of your code.

Categories

Resources