用确认框停止我的 while 循环

Stopping my while loop with a confirm box

本文关键字:我的 while 循环 确认      更新时间:2023-09-26

这是我的代码。 我无法弄清楚当使用从窗口中选择"取消"时如何停止此循环。

我真的很生疏,从来没有深入研究过编程,哈哈 - 我习惯C++所以我不确定除了 window.conconfirm 之外是否有更好的函数可以调用。

法典:

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

提前感谢!

代码中的小错误:

while (askAgain = true){

应该是:

while (askAgain == true){

= 赋值== 检查它们是否相等

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

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