Why doesn't "i" always equal zero? Do subsequent iterations of the loop skip the var declaration? I know that at the end of the first iteration i = 1 but when it goes through subsequently does the var declaration not reset i = 0?
When I run the function with args it all works but I just don't know why the var declaration doesn't reset if that makes any sense. It would make sense to me if the var "i" was declared outside of the loop.
function multiply(arr, n) {
var product = 1;
for (var i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
The first section of a for loop declaration is (for the most part) only run once, at the very beginning of the loop. Your code is equivalent to:
function multiply(arr, n) {
var product = 1;
var i = 0;
for (; i < n; i++) {
product *= arr[i];
}
return product;
}
Which is equivalent to:
function multiply(arr, n) {
var product = 1;
var i = 0;
while (true) {
// for loop body:
product *= arr[i];
// for loop declaration part:
i++;
if (!(i < n)) break;
}
return product;
}
So, i is only assigned 0 once, at the very beginning.
I qualified the above with for the most part because, when the behavior is somewhat stranger when a variable is declared with let, and let has block scope, not function scope - with let, every iteration creates a separate binding for the variable name. (but that's not worth worrying about in your case - it's only really something that needs to be considered when the for loop uses the variable asynchronously)
Related
Why can't I declare a variable inside a for loop below?
Wrong:
for(let i = 1; i <= num; i++){
let factorials = [];
if(num % i === 0){
factorials.push(i);
}
}
// ReferenceError: factorials is not defined
Correct:
let factorials = [];
for(let i = 1; i <= num; i++){
if(num % i === 0){
factorials.push(i);
}
}
If you define a variable inside of { }. It means that the variable is only alive inside of the { }. It's called a scope.
You have a scope using if/while/for/function...
In the following example, the factorials variable is alive only inside of one loop iteration of the for. Which means that you are recreating a factorials variable every time you loop.
for(let i = 1; i <= num; i++){
const factorials = [];
if(num % i === 0){
factorials.push(i);
}
}
If you want to make it work, you have to create your variable outside of the for so it will not be recreate after every iteration, and keep it's values.
const factorials = [];
for(let i = 1; i <= num; i++){
if(num % i === 0){
factorials.push(i);
}
}
In the first code snippet, you are declaring factorials inside the for loop block, hence it will not be visible outside the for loop block.
It is working in the 2nd snippet because you declared it outside of block.
Why can't I declare a variable inside a for loop below?
No, you can.
Here is the proof.
let num = 5;
for (let i = 1; i <= num; i++) {
let factorials = [];
if (num % i === 0) {
factorials.push(i);
}
}
let factorials = [];
for (let i = 1; i <= num; i++) {
if (num % i === 0) {
factorials.push(i);
}
}
console.log("No Error.")
Yes, you can, but if you declare the factorials-array in the loop it's only available in that closure. So consoling it out of the loop won't work.
Here's a very informative article about closures:
https://javascript.info/closure
The whole javascript.info-site is very interesting, I am studying it for now to strengthen my javascript basics and fill not known informations-holes, although I am into web developing since years. But it never hurts to get more knowledge, even if its the basics.
Also I would advice you to use "use strict;" from beginning when developing, it helps to keep your scripts more clean, disallowing sources of errors.
If reverse == true I want to run one kind of loop, else I want to run another one.
Currently, here is an example of my code:
if (reverse) {
for (var i = 0; i < length; i++) {
...
}
} else {
for (var i = length; i >= 0; i--) {
...
}
}
The code inside is really big, and is quite the same. I could use a function, but this function would have so many params that is not a good choice.
So I've tried to do something like that:
var loopStart1 = startPageIndex;
if (!reverse) {
condition1 = function(i) {
return i < length;
}
increment1 = function(i) {
return ++i;
}
} else {
condition1 = function(i) {
return i >= 0;
}
increment1 = function(i) {
return i--;
}
}
mainLoop: for (var i = loopStart1; condition1(i); increment1(i)) {
But now I have an infinite loop.
Any idea on how to solve this issue?
Why not do it inline?! ;)
var start = startPageIndex;
for (var i = start; (reverse && i >= 0) || (!reverse && i < length); reverse ? --i : ++i) { }
Assuming the specific case is to traverse through an array either backwards or forwards, there are two simple ways to do that.
1) Just conditionally reverse the array before looping, or
2) Use a single loop but conditionally map the loop variable to something else. Something like...
for (var i = 0; i < length; i++) {
var j = i;
if (reverse) {
j = length - (i + 1);
}
// ...then use j instead of i
}
If you want to make it dynamic, I wouldn't use a for loop, but a do..while loop to be easier to customize.
Your main function would just have a simple reverse bool flag and you could just pass that.
Inside that function that you want to depend on the reverse flag, you can use the ternary expression in the condition (x ? y : z)
Makes it clearer to read. In theory you can do it in a for loop directly, using two ternary directions.
do {
//Your actions here, using i as counter
//Here you will do the counter direction change
if(reverse)
i--;
else
i++;
// Use ternary expression if (x ? y : z) in the while condition
// Reads as: If x is true, do y, else do z
// All in one line
} while(reverse ? i>=0 : i<n);
Ideally, in these situations I would consider using iterators.
How about a simple loop function,
Below I've created a simple function called myLoop, were you can pass the length, if it's reversed, and what callback you want for each loop iteration.
eg.
function myLoop(length, reverse, each) {
var index;
if (!reverse) {
for (index = 0; index < length; index ++) each(index);
} else {
for (index = length -1; index >= 0; index --) each(index);
}
}
function show(index) {
console.log("Loop index = " + index);
}
console.log("forward");
myLoop(5, false, show); //normal
console.log("revere");
myLoop(5, true, show); //reversed
I would opt for the same code, just change the array.
var array = ['one', 'two', 'three'];
var reversed = true;
let arrToUse = reversed ? array.reverse() : array;
for (var i = 0; i < arrToUse.length; i++) {
console.log(arrToUse[i]);
}
Check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
Primitive parameters (such as a number) are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function.
This is what happens when you call increment1(i), outer i in for loop is not changed. To fix it just set i from increment1(i)'s return value.
mainLoop: for (var i = loopStart1; condition1(i); i = increment1(i)) {
I run the two following code snippets.
The first loop is giving the expected result when I assign a different name for the local variable.
for(var i = 0; i < 3; i++) {
setTimeout((function() {
var i2 = i;//named i2 here
return function(){console.log(i2)};
})(), 10);
}
The second loop will print undefined instead. I thought
var i = i will just re-declare the original i. And I expect it pint out some number.
How come I get undefined here?
for(var i = 0; i < 3; i++) {
setTimeout((function() {
var i = i;
console.log(i);
return function(){console.log(i)};
})(), 10);
}
The scope of the initialization expression of var is the body of the function, and the local variable i is already in that scope. So you can't reference an outer variable with the same name. You can think of
var x = <expression>;
as equivalent to:
var x;
x = <expression>;
If you look at it this way, you can see why var i = i; won't work, it's equivalent to:
var i;
i = i;
The assignment uses the uninitialized value of the local variable.
The usual idiom to solve this is to make i a parameter to the function, which you then pass in the argument list of the IIFE.
for(var i = 0; i < 3; i++) {
setTimeout((function(i) {
console.log(i);
return function(){console.log(i)};
})(i), 10);
}
For more information, see JavaScript closure inside loops – simple practical example
Scope
The inner var i declares a new variable called i within a scope that already has a variable called i.
The declaration is assigned the value of the scope's most prominent i, which is the one you just declared.
Since the value of the newly declared i is undefined, the assignation of itself to itself is of its value of undefined.
The variable i is already being used by the loop for in the outer scope. Now you are declaring a new variable i in an inner scope
var i = i;
Once that statement is ran the inner scope is the counter i of the loop is more accessible because you overridden it with new i.
Basically what you are doing here is: defining a new variable i and assign to it a value of that variable you just declared which is undefined.
Eaiset solution is to declare j and assign to it the value i.
for(var i = 0; i < 3; i++) {
setTimeout((function() {
var j = i;
console.log(j);
return function(){console.log(j)};
})(), 10);
}
OR just use i while passing it the function setTimeout
for(var i = 0; i < 3; i++) {
setTimeout((function(i) {
console.log(i);
return function(){console.log(i)};
})(), 10);
}
My guess is it should be return function(){console.log(i)}; in the second snippet. And the reason it still does not work is, that you're basically assigning to var i the same i you just declared with var. You'll see what I mean if you split declaration and assignment:
var i;
i = i;
But yeah, I can see how js quirks like that can be quite frustrating, because one has to always watch out and reinvent new variable names as one goes down the call stack. This is one reason why I fell in love with TypeScript, because it compiles this:
for (let i = 0; i < 3; ++i) {
window.setTimeout(() => {
console.log(i);
});
}
into this:
var _loop_1 = function (i) {
window.setTimeout(function () {
console.log(i);
});
};
for (var i = 0; i < 3; ++i) {
_loop_1(i);
}
which will yield the expected result.
The first function determines if a number is prime. The second function is supposed to create an array with all prime numbers up to and including the max value, but it gives me an infinite loop for some reason.
function isPrime(num) {
for (i = 2; i < num; i++) {
if (num % i === 0) {
return false
}
}
if (num <= 1) {
return false;
}
return true;
}
function primes(max) {
var all = [];
for (i = 2; i <= max; i++) {
if (isPrime(i)) {
all.push(i);
}
}
}
primes(17);
Your i variable is global, so both functions use the same i. This means the first function changes it while the second one is looping.
As the first function will have set i to num-1 when it finishes, and num was the value of i before executing it, it effectively decrements i with one. And so i will get the same value in the next iteration of the loop in the second function, never getting forward.
Solve this by putting the var keyword in both functions.
for(var i=2; // ...etc)
The variable i in your two loops are global variables and they overwrite each other so the first loop never ends.
In ur code problem is with the scope of variable i, In prime no calculation, u can check upto the sqrt of no, if no is not divisible by any no upto its sqrt, then it will a prime no, Try this:
function isPrime(num) {
let k = Math.sqrt(num);
for (let i = 2; i <= k; i++) {
if (num % i === 0) {
return false
}
}
return true;
}
function primes(max) {
let all = [];
for (let i = 2; i <= max; i++) {
if (isPrime(i)) {
all.push(i);
}
}
console.log(all)
}
primes(17);
primes(25);
I have a for loop but it gets executed once where it should execute two times.
The $appointments variable holds the returned data from an ajax call. When I console.log($appointments.length) I get two which is correct but the iteration happens only once and I can't figure out why.
for (var i = 0; i < $appointments.length; i+=1) {
var rangeStart_hour = $appointments[i].timerange.substring(0, 2);
var rangeStart_minutes = $appointments[i].timerange.substring(3, 5);
var rangeEnd_hour = $appointments[i].timerange.substring(11, 13);
var rangeEnd_minutes = $appointments[i].timerange.substring(14, 16);
var rangeS = rangeStart_hour + rangeStart_minutes;
var rangeE = rangeEnd_hour + rangeEnd_minutes;
var rangeStart = parseInt(rangeS);
var rangeEnd = parseInt(rangeE);
var range=0;
if(parseInt(rangeStart_hour) == 12){
if(parseInt(rangeStart_minutes) == 0){
range = rangeEnd - 0;
}else{
range = rangeEnd - (parseInt(rangeStart_minutes)+20);
}
}else{
if(parseInt(rangeStart_minutes) == 30 && parseInt(rangeEnd_minutes) == 0){
// if start time 1:30 ,end time 3:00
range = (rangeEnd - (rangeStart + 20)); // 300 - 150 = 150
}else if(parseInt(rangeStart_minutes) == 0 && parseInt(rangeEnd_minutes) == 30){
range = ((rangeEnd+20) - rangeStart);
}else{
range = rangeEnd - rangeStart;
}
}
console.log(range);
for(var i=1; i<(range/50); i++){
$("a[data-time='"+(rangeStart)+"']").addClass('time');
rangeStart += 50;
};
};
This structure seems like a bad idea:
for (var i = 0; i < $appointments.length; i += 1) {
//...
for (var i = 1; i < (range/50); i++){
//...
}
}
You probably want to use different variable names for your nested loop counters.
You reinitialize the variable i in the second for loop, if you change
for(var i=1; i<(range/50); i++){
$("a[data-time='"+(rangeStart)+"']").addClass('time');
rangeStart += 50;
};
to use a different variable, say j it should work fine.
In JavaScript, the scope of variable is function scope. It means that all variable declarations are hoisted to the top of the function.
So, in your case:
for (var i = 0; i < $appointments.length; i += 1) {
...
for (var i = 1; i < (range/50); i++){
...
};
};
equals with
var i; // two variables with the same name are hoisted as the same variable.
for (i = 0; i < $appointments.length; i += 1) {
...
for (i = 1; i < (range/50); i++){
...
};
};
So, at the end of the second loop, "i" will be greater than or equal to range/50. If this value is not less than $appointments.length, the first loop will terminate after the first round.
Important: JavaScript does not have block scope. Variables introduced with a block are scoped to the containing function or script, and the effects of setting them persist beyond the block itself. In other words, block statements do not introduce a scope. Although "standalone" blocks are valid syntax, you do not want to use standalone blocks in JavaScript, because they don't do what you think they do, if you think they do anything like such blocks in C or Java.
In your code the "i" variable used twice, So second time initialized value is Incrementing in first for loop. That's why, Condition failed in first for loop and loop is terminating. Change the variable name in second or first for loop then it will be working as you expected