使用确认框停止我的while循环

时间:2013-01-25 22:21:16

标签: javascript while-loop

这是我的代码。当用户从window.confirm框中选择“取消”时,我无法弄清楚如何停止此循环

我真的很生疏,从来没有深入研究过任何大声的编程 - 我已经习惯了C ++所以我不确定是否可能有更好的函数来调用window.confirm以外的其他函数。

代码:

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;
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:5)

代码中的小错误:

while (askAgain = true){

应该是:

while (askAgain == true){

=指定一个值 ==检查它们是否相等

另一个问题是你需要像这样分配确认:

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