javascript - why is this while loop causing an infinite loop? -
for reason i'm having difficulty getting while loop work. keeps crashing browser whenever try test out, , in 1 case able see results of loop in console, saw nan printed several times. there i've forgotten in code?
<div id="output"></div> <script> var starting = prompt("what starting balance?"); var target = prompt("what target balance?"); var interest = prompt("what 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 make starting balance greater target balance."); document.queryselector('#output').textcontent = "to grow initial investment of " + starting + " " + target + " @ " + interest + " interest rate require " + periods + " investment periods."; } } </script>
you forgetting convert result prompt string number.
var starting = parsefloat(prompt("what starting balance?"));
do same thing other numbers input user prompt.
Comments
Post a Comment