Jpeppa
Jpeppa

Reputation: 11

Stopping my while loop with a confirm box

Here is my code. I can't figure out how to stop this loop when the use selects "cancel" from the window.confirm box

I'm really rusty and never delved that deep into programming anywho lol - I am used to C++ so I'm not sure if there just might be a better function to call other than window.confirm.

code:

alert( "1 = 10, 2 = 20, 3 = 30, 4 = 40, 5 = 50" );

var myArray = [0, 10, 20, 30, 40, 50];
var askAgain = true;
var pickOne;
var pickTwo;
var answer;
var numOne;
var numTwo;

while (askAgain = true){

    numOne = prompt( "Select a number by entering the associated slot #:");
    numTwo = prompt( "Select a number to be added to the first number by            

        entering the associated slot #:" );

    pickOne = myArray[numOne];
    pickTwo = myArray[numTwo];

    pickOne.Number;
    pickTwo.Number;

    answer = pickOne + pickTwo;

    alert("You picked " + pickOne + " and " + pickTwo + ", combined they equal: "       

        + answer);

    window.confirm( "Would you like to go again?" );
    if (confirm == true){
        askAgain = true;
    } else {
        askAgain = false;
    }
}

Thanks in advance!

Upvotes: 0

Views: 4772

Answers (1)

nathan hayfield
nathan hayfield

Reputation: 2685

Small error in your code:

while (askAgain = true){

Should be:

while (askAgain == true){

= assigns a value == checks if they are equal

another issue is you need to assign confirm like this:

var confirm = window.confirm( "Would you like to go again?" );

Upvotes: 5

Related Questions