1

In the following function I need to increment the years variable in order to find the amount of years that need to pass before I reach a desired profit. I noticed that this function does not work if I use years++ instead of ++years. I understand the difference between the two methods of increment, however, I still do not understand while in this particular case years++ causes the loop to be executed only once.

 function calculateYears(investment, interestRate, tax, desiredProfit) {
        var years = 0;
        while(investment < desiredProfit && ++years){
          investment += (investment * interestRate) * (1 - tax);
        }
    
        return years;
    }
    var years = calculateYears(1000, 0.05, 0.18, 1100);
    console.log(years);

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

7 Comments

It returns 4 instead of 3. See my answer
@MaxMaximilian Thanks for pointing out, there will be an extra increment while checking for final while, I have made the updates.
@gurvinder372 your explanation is very clear and useful. Thank you! I made a test when changing var years = 1. It gave interesting result - while loop works, which is perfect. But it does not give the logic needed - as the value of investment in the first loop need to be taken as the one before the first year passed. In conclusion, for the function to give proper result I need to initialize it with 0. But having your explanation, as well as answers from others, I can understand now why the things work the way they work. Thanks a lot!
I think, that way isn't very clean. That works, which is good, but 1) it makes unnecessary calculations after the loop, 2) on each loop it checks && years++ which makes unnecessary calcs too. As programmers we should make code cleaner, because that can show up in bigger projects where you need to create something which does perform better in speed etc. But yeah, that explains the root of the issue, so good answer gurvinder372
@MaxMaximilian I agree that this isn't the cleanest and there can be more readable ways of doing this.
|
4

It gets executed only once because the value of years used for checking truthiness is 0, i.e. before incrementing.

MDN Documentation

Comments

2

You should use years++ inside the loop:

function calculateYears(investment, interestRate, tax, desiredProfit) {
    var years = 0;
    while(investment < desiredProfit){
      investment += (investment * interestRate) * (1 - tax);
      years++;
    }

    return years;
}
alert(calculateYears(1000, 0.05, 0.18, 1100));

JSFIDDLE

Comments

0
while(investment < desiredProfit && ++years)

this condition will not execute because as years is 0 and it increment after the 1 loop so it is like

desiredProfit && 0 -> false/0

and after

investment < 0 //is always false

Comments

0

You did forget that your while loop is not incrementing the value of years

 while(investment < desiredProfit && ++years){
  investment += (investment * interestRate) * (1 - tax); years++;
}

Comments

0

Why not do something like this

let i = -6;
let end = 9;
while ((i++ || true) && i < end) {
...do something...
}

Comments

-1

Because && years++ translates to && 0 which will translates to false value.

If you want to use years++, initialize years to 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.