最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Stopping my while loop with a confirm box - Stack Overflow

matteradmin2PV0评论

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 + ", bined they equal: "       

        + answer);

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

Thanks in advance!

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 + ", bined they equal: "       

        + answer);

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

Thanks in advance!

Share Improve this question edited Jan 25, 2013 at 22:38 MadSkunk 3,7492 gold badges36 silver badges52 bronze badges asked Jan 25, 2013 at 22:21 JpeppaJpeppa 112 silver badges5 bronze badges 2
  • Where is the value for confirm being assigned? Also, you should be able to merge the askAgain and confirm variables into a single variable. – Dave Jarvis Commented Jan 25, 2013 at 22:29
  • @DaveJarvis It's not, at least not in the code shown here... :) – MadSkunk Commented Jan 25, 2013 at 22:30
Add a ment  | 

1 Answer 1

Reset to default 5

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?" );
Post a comment

comment list (0)

  1. No comments so far