Code runs with one answer to prompt but not another


#1


I got the code to work but only when the answer to the first prompt is yes. If the answer is no the page becomes unresponsive, which I'm assuming means an infinite loop, but I can't find it in my code. I'm also positive there are many redundancies in my code or more effective ways to get my game to work, but for now my biggest issue is finding what's causing that infinite loop.

var firstQuestion = prompt("Do you love me now? It's a simple yes or no question.")

var firstAnswer = function(firstQuestion) {
    do {
        console.log("Is that so?");
    }
    while (firstQuestion === "no");
    firstQuestion = "yes"
    console.log("I knew you'd make the right choice ;) !");
};

firstAnswer(firstQuestion);

var beckon = prompt("Won't you come a little closer?");

var secondAnswer = function(beckon) {
    while (beckon === "okay") {
        console.log("Just a little closer...");
        beckon = 0;
    };
};

secondAnswer(beckon);

for (i=0; i <= 5; i++) {
    console.log("One more step.");
};

console.log("Together at last!");


#2

The page becomes unresponsive because you are creating an infinite loop,

  1. You need to think care fully about your while condition, and it is highly inadvisable to use the same parameter name as the variable you are parsing to your function,

Also your normal while loop will not run properly because of your while condition and again same name parameter as function argument,

var beckon = prompt("Won't you dance with me? (okay or no) ");

not_done = true
var secondAnswer = function(b) {
    while (not_done) {
        console.log("Just a little closer...");
        not_done= false;
    };
};

secondAnswer(beckon);

The code above is my take on your while loop, use this as a reference in creating your do..while and while. :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.