2

For some reason I'm having difficulty getting this while loop to work. It keeps crashing my browser whenever I try to test it out, and in the one case that I was able to see the results of the loop in the console, all I saw was NaN printed several times. Is there something I've forgotten in my code?

<div id="output"></div>

<script>

var starting = prompt("What is your starting balance?");
var target = prompt("What is your target balance?");
var interest = prompt("What is your interest rate?");
var periods = 0;

var current = starting;
var greaterThan = false;

while (greaterThan === false) {
  if (current < target) {
    current = current + (current * interest);
    periods++;
  } else {
    greaterThan = true;
    alert("it took " + periods + " periods to make your starting balance greater than your target balance.");
    document.querySelector('#output').textContent = "to grow an initial investment of " + starting + " to " + target + " at a " + interest + " interest rate will require " + periods + " investment periods.";
  }
}

</script> 
2
  • Did you try and set a breakpoint at greaterThan = true and verify the code reaches this far ? Commented Sep 30, 2015 at 5:07
  • You have to cast the string to an int.(parseInt) Commented Sep 30, 2015 at 5:11

3 Answers 3

5

The one problem I could see is, all your input values are string, not numbers so they are doing string comparison not numeric

var starting = +prompt("What is your starting balance?") ||0;
var target = +prompt("What is your target balance?")||0;
var interest = +prompt("What is your interest rate?")||1;

The + in front of prompt() is the unary plus operator

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

3 Comments

+null returns 0. Much better than parseFloat since it wont return NaN.
@hani only if that's your desired behavior. One may not want to convert null to zero because null means they hit cancel. How are you supposed to tell the difference between typing 0 and hitting cancel?
@Arun P Johny In this case you are doing an OR which means if they hit cancel they still get 0 (null || 0). There is also a plus before prompt, which again makes it 0 if they hit cancel (+null)
4

You are forgetting to convert the result from prompt from a string into a number.

var starting = parseFloat(prompt("What is your starting balance?"));

Do the same thing to the other numbers that are input by the user from the prompt.

Comments

0

First you need to convert your input into an integer value. The input from the prompt is a string. even if you enter 1 or 10 or any number. You can use parseInt() for that. and because you are asking for interest rate, i think any user would enter something like 2. 5, or 10 as a percentile. not 0.1, or 0.05. Even if he does, the parseInt() function can't get it right because 0.05 is not an integer value. You can use parseFloat for that. so i suggest you look at my implementation of your code below. also, i have omitted the if else statements because they weren't necessary and would only make the code more complex.

<div id="output"></div>

<script type="text/javascript">

var starting = parseInt(prompt("What is your starting balance?"));
var target = parseInt(prompt("What is your target balance?"));
var interest = parseInt(prompt("What is your interest rate?"));
var periods = 0;
var intrate = interest/100;
var current = starting;

while (current< target) {

    current += (current*intrate);
    periods += 1;
  } 
    alert("it took " + periods + " periods to make your starting balance greater than your target balance.");
    document.querySelector('#output').textContent = "to grow an initial investment of " + starting + " to " + target + " at a " + interest + " interest rate will require " + periods + " investment periods.";
</script>

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.