0

The loop is supposed to take the price of each book, add it to the total, and then put the average on the page for each book until the user enters in "N"

<script type="text/javascript">
var ct = 1;
var yesORno = "Y";
while (yesORno = "Y"){
    book = prompt("What is the price of book #" + ct, 0);
    total = parseInt(book) + total;
    ans = total / ct;
    document.write("<p>With book #" + ct +" The average is " + ans + "</p>");
    ct = ct + 1;
    yesORno = prompt("Would you like to continue? (Y/N)", "")
}
</script>
3
  • 1
    That's an infinite loop. Commented Oct 28, 2013 at 1:20
  • 2
    while (yesORno = "Y"){ should be while (yesORno === "Y"){ Use jshint.com to find common errors in your code. Commented Oct 28, 2013 at 1:20
  • (optional) you should also check for "y" Commented Oct 28, 2013 at 1:32

3 Answers 3

8

You should change your while condition to:

while (yesORno == "Y")

Using only = will make it assign 'Y' value to yesORno and return itself, which is evaluated as true and makes it run forever.

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

Comments

3
var ct = 1;
var yesORno = "Y";
while (yesORno == "Y"){
    book = prompt("What is the price of book #" + ct, 0);
    total = parseInt(book) + total;
    ans = total / ct;
    document.write("<p>With book #" + ct +" The average is " + ans + "</p>");
    ct = ct + 1;
    yesORno = prompt("Would you like to continue? (Y/N)", "")
}

Look at the third line.

Comments

3

Like others stated, you have used the assignment operator = instead of the equality operator == or the strict equality operator ===.

However, you could also refactor your code using a do while loop instead. That would remove the need of having a yesORno variable.

do {
    //...
} while(prompt("Would you like to continue? (Y/N)", "") === 'Y')

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.