-3

I have five questions to a user that require user answers. Correct answers are counted and then the user is graded at the end.

I would like the grading of each questions to appear immediately after each respective question, not after all have been asked. As it is now, it asks 2 out of 5 questions and then provides a response to the user but only after the questions are all asked. The order is: (ask, ask, answer, answer) but I need it to be (ask, answer, ask, answer) but I can't figure out why it's not doing that.

// Counters
var numberOfQuestions = 5;
var correctAnswers = 0;

// The five questions
var raining = prompt("Is it raining today?");
var married = prompt("Am I married?");
    /* Remaining questions
    var day = prompt("What day is it?");
    var threes = prompt("What does 3 + 3 + 3 equal?");
    var number = prompt("What number am I thinking of?"); 
    */

// First question - Is it raining
if (raining.toUpperCase() === "NO") {
  correctAnswers += 1;
  alert("Correct. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);  
} else {
    alert("Don't you wish it was. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);
  }

// Second question - Am I married
if (married.toUpperCase() === "YES") {
  correctAnswers += 1;
  alert("Correct! I am married. \nYou now have " + correctAnswers + " correct answers out of " + numberOfQuestions);
} else {
  alert("Incorrect, I am married. \n You still have " + correctAnswers + " correct answers out of " + numberOfQuestions);
}

Any help would be most appreciated.

admdrew
  • 3,790
  • 4
  • 27
  • 39
Padawan
  • 724
  • 1
  • 8
  • 20
  • 1
    The order is as you can see in the code. You didn't expect that? `prompt` is ask, `alert` is answer. – Rudie Nov 04 '14 at 19:54
  • Just put a question (prompt) before the part that is checking its answer. Now, all the questions are asked first and checked when all questions are answered. – Barry Nov 04 '14 at 19:56
  • @Padawan Downvote as, per the downvote mouseover, "this question does not show any research effort." Standard debugging (a tool that everyone, especially beginners, should have in their programming repertoire) could've revealed the problem to you. – admdrew Nov 04 '14 at 20:39
  • @admdrew Negative 3 points. Awesome. Come on, I'm sure you can do more to teach me a lesson for not properly researching before posting. Correct, I didn't, and you are right I should have used a dev tool, which sadly is common with noobs. Alas, I am also sure that the only way to deal with such ignorance and impudence is to downvote the hell out of them instead of reminding them of the need to research prior to posting. Downvote away admdrew. – Padawan Nov 04 '14 at 20:48
  • @Padawan I'm sorry you feel that way, but that's one major reason downvotes exist. You'll notice [my only SO question](http://stackoverflow.com/questions/19640531/qtp-uft-adding-comments-to-code-in-editor-view) also received a number of downvotes, probably also due to lack of research. Ultimately you can't let it bother you, and there are *tons* of opportunities for you to raise your rep in ways other than asking questions. – admdrew Nov 04 '14 at 20:52
  • 1
    @Padawan I didn't downvote and my answer is like I always talk. No more letters than necessary. If that's rude, I'm fine with that. – Rudie Nov 04 '14 at 20:53
  • @adndrew - It didn't occur to me to use a dev tool till after you mentioned it, which made me feel quite foolish to be honest, since I should have. But, the downvotes didn't help me at all in reminding me of this. It just reinforced the stereotype that this site has managed to earn. If your goal is to have people follow protocol, then I suggest mentioning it in the comments whilst downvoting. Downvoting without a simple reminder appears to be more of a show of arrogance than anything else and doesn't help the cause. From here on out, I'll do my due diligence, thankyouverymuch. – Padawan Nov 04 '14 at 21:02
  • I understand feeling the way you do - just try to keep in mind that votes are for *content* not *users*, so they really aren't intended to be personal. – admdrew Nov 04 '14 at 21:06

4 Answers4

1

Just place

var married = prompt("Am I married?");

before the second if.

Assigning variable to prompt() shows prompt immediately and not when you start doing some operations with this variable.

nicael
  • 18,550
  • 13
  • 57
  • 90
1

Quite simply put:

var married = prompt("Am I married?");

after:

// Second question - Am I married
ronnydw
  • 923
  • 16
  • 24
0

The simple way would be to alternate the if-checks between the prompts. However, you may want to use functions to help keep things straight:

function ask(question, correct_answer, correct_text, incorrect_text) {

    var answer = prompt(question);
    
    if (answer.toUpperCase() === correct_answer.toUpperCase()) {
        alert(correct_text + "\nYou have " + (++correct_count) + " out of " + question_count);
    } else {
        alert(incorrect_text + "\nYou have " + (correct_count) + " out of " + question_count);
    }

}

function run() {
    correct_count = 0;
    question_count = 2;

    ask("Is it raining?", "YES", "Correct, it is!", "Sorry, but it actually is.");
    ask("Am I married?", "YES", "Correct, I am!", "Sorry, but I've got the ring and everything.");

    resolve();
}

function resolve() {
    alert("You got " + correct_count + " questions right!");
}

run();
Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
-1

This should work. It is based on your original code.enter code here

var raining = prompt("Is it raining today?");

if (raining.toUpperCase() === "NO") {
  correctAnswers += 1;
  alert("Correct. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);  
} else {
    alert("Don't you wish it was. \nYou have " + correctAnswers + " correct answers out of " + numberOfQuestions);
  }


var married = prompt("Am I married?");
    /* Remaining questions
    var day = prompt("What day is it?");
    var threes = prompt("What does 3 + 3 + 3 equal?");
    var number = prompt("What number am I thinking of?"); 
    */



// Second question - Am I married
if (married.toUpperCase() === "YES") {
  correctAnswers += 1;
  alert("Correct! I am married. \nYou now have " + correctAnswers + " correct answers out of " + numberOfQuestions);
} else {
  alert("Incorrect, I am married. \n You still have " + correctAnswers + " correct answers out of " + numberOfQuestions);
}
larisoft
  • 191
  • 2
  • 5