I still do not understand while in this particular case years++ causes the loop to be executed only once.
because && years++ translates to && 0 which will translates to falsey value.
If you want to use years++, initialize years to 1
function calculateYears(investment, interestRate, tax, desiredProfit) {
var years = 1;
while(investment < desiredProfit && years++){
investment += (investment * interestRate) * (1 - tax);
}
return years - 1;
}
console.log(calculateYears(1000, 0.05, 0.18, 1100));
Answer from gurvinder372 on Stack OverflowI still do not understand while in this particular case years++ causes the loop to be executed only once.
because && years++ translates to && 0 which will translates to falsey value.
If you want to use years++, initialize years to 1
function calculateYears(investment, interestRate, tax, desiredProfit) {
var years = 1;
while(investment < desiredProfit && years++){
investment += (investment * interestRate) * (1 - tax);
}
return years - 1;
}
console.log(calculateYears(1000, 0.05, 0.18, 1100));
It gets executed only once because the value of years used for checking truthiness is 0, i.e. before incrementing.
MDN Documentation
while loop increment in Javascript - Stack Overflow
increment variable in while loop
javascript - Can a for loop increment/decrement by more than one? - Stack Overflow
increment in for loop javascript - Stack Overflow
Videos
((x+=2) < n)
x+=2 is a shorthand for x=x+2;
if x=0 initially the condition that would be checked would be 2 < n
(x < n) {...x += 2}
if x=0 initially the condition that would be checked would be 0 < n
Main difference is you are incrementing first and then checking the condition in first one while in second one you check and then increment x.
So your question is what's the difference between...
while ((x+=2) < n) {}
AND
while(x < n) { x+=2 }
The main difference is that in the first example, the x gets the 2 added on and is then compared to the value of n, whereas in the second example, x is compared to n before the 2 has been added and will therefore most likely run 1 more loop than you would expect
Use the += assignment operator:
for (var i = 0; i < myVar.length; i += 3) {
Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.
For more information about each step of the for loop, check out the MDN article.
A for loop:
for(INIT; TEST; ADVANCE) {
BODY
}
Means the following:
INIT;
while (true) {
if (!TEST)
break;
BODY;
ADVANCE;
}
You can write almost any expression for INIT, TEST, ADVANCE, and BODY.
Do note that the ++ operators and variants are operators with side-effects (one should try to avoid them if you are not using them like i+=1 and the like):
++imeansi+=1; return ii++meansoldI=i; i+=1; return oldI
Example:
> i=0
> [i++, i, ++i, i, i--, i, --i, i]
[0, 1, 2, 2, 2, 1, 0, 0]
You're declaring the variable inside the loop, so it happens every time the loop runs. Simply move the declaration outside...
var number = 0;
for (var i = 0; i < 5; i++) {
number++;
console.log(number);
}
It prints 1, 1, 1, 1, 1 because you reset numberin every iteration.
change your code to:
for (var i = 0; i < 5; i++) {
console.log(i);
}
or
var number = 0;
for (var i = 0; i < 5; i++) {
console.log(number);
number++;
}
(Answer to additional question in comments) You get 1,2,3,4,5 because you increment number before you print it.