Kim Curran
Kim Curran

Reputation: 25

why is this while loop causing an infinite loop?

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> 

Upvotes: 2

Views: 113

Answers (3)

Vivek Tiwari
Vivek Tiwari

Reputation: 21

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>

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

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

Upvotes: 5

Ruan Mendes
Ruan Mendes

Reputation: 92274

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.

Upvotes: 4

Related Questions