0

The function nb_year should return n number of entire years needed to get a population greater or equal to p.The given parameters are p0(original population), percent(percentage of increase), aug (inhabitants coming or leaving each year), p (population to surpass) i understand what the math is to get the result i just don't know what type of loop i should write to keep increasing the numbers. here is my current code

function nbYear(p0, percent, aug, p) {
   var increaseInHab = p0 * (percent * .01);
   var currentInhab = increaseInHab + p0 + aug;

   for(var n = 0;currentInHab < p;n++) {
     console.log(n)
     n++
     console.log(currentInhab + increaseInHab);
     currentInhab + increaseInHab;
    }
   return n;
}
11
  • currentInhab + increaseInHab; adds two numbers and does nothing with the result. Hint, you need to store it back into the variable you want to update. Commented Jan 10, 2017 at 22:18
  • It should be avoided in general, use a JSON-param instead. Commented Jan 10, 2017 at 22:18
  • 2
    @Blauharley Are we reading the same question???? What is a "JSON-param" Commented Jan 10, 2017 at 22:19
  • Recursion is the process generally used when modeling populations. Commented Jan 10, 2017 at 22:19
  • "aug (inhabitants coming or leaving each year)" aug is expected to be position number, negative number or 0? "i just don't know what type of loop i should write to keep increasing the numbers." If aug can be a negative number, the numbers are not guaranteed to increase, yes? Commented Jan 10, 2017 at 22:19

2 Answers 2

2

Good loop, but implementation needs fixing.

function nbYear(p0, percent, aug, p) {
   var increaseInHab = p0 * (percent * .01);
   var currentInhab = increaseInHab + p0 + aug;

   for(var n = 0; currentInHab < p; n++) {
     currentInhab += increaseInHab;
   }

   return n;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Without looking at the specific details of your code (I'm assuming the multiplication expressions are correct), below would be a quick fix for your for loop problems:

function nbYear(p0, percent, aug, p) {
    var increaseInHab = p0 * (percent * .01);
    var currentInhab = increaseInHab + p0 + aug;
    var n = 0;

    for (; currentInHab < p; n++) {
       currentInhab + increaseInHab;
    }

    return n;
}

Also, you were incrementing n twice.

5 Comments

I'm confused. I did not use let
You assumed that var works the way let does, but it doesn't. Your code was identical to the OP's before the edit and now it just has one less n++ and the logging is removed, but moving the declaration of n has no effect..
I don't see let being used in my original answer, edited answer, nor the author's original question.
Exactly, var is used in both the OP's post and your answer, not let, so it doesn't matter where in the function n is declared. It would only matter if let was used, but it isn't.
I understand what you are trying to communicate now. I did not before.

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.