Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Exclamation Please help with my trivia game

    This is a basic trivia game I am currently building, but the user's answer is treated as incorrect whether it is correct or not. I think the problem is with the "if" statement, but I'm not sure. Please take a look. Thanks.

    [CODE]

    var question = 0;
    var correctAnswer = 0;
    var score = 0;
    var questionNumber = 0;
    var userAnswer = 0;



    var playerOne = {
    score: 0,
    };

    var playerTwo = {
    score: 0,
    };




    var randomize = function(){
    questionNumber = (Math.floor(Math.random()*3+1));
    switch(questionNumber){
    case 1:
    question = "What is the largest country on Earth?";
    correctAnswer = "russia";
    break;
    case 2:
    question = "What is the meaning of life?";
    correctAnswer = "42";
    break;
    case 3:
    question = "What is the capital of Columbia?";
    correctAnswer = "bogota";
    break;
    };
    };

    var letsPlay = function(player) {
    randomize();
    userAnswer = prompt(question).toLowerCase;
    if(userAnswer === correctAnswer){
    player.score = player.score + 10;
    alert("That's right! Could you beee more correct!??" + " " + "Your score is" + " " + player.score);
    }else{
    player.score = player.score + 0;
    alert("Ooooh No! Wrong answer!" + " " + "Your score is" + " " + player.score);
    };
    };

    letsPlay(playerOne);
    letsPlay(playerTwo);
    letsPlay(playerOne);
    letsPlay(playerTwo);

    [CODE]

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Have you tried using your error console?

    Have a look at this line:-

    userAnswer = prompt(question).toLowerCase;
    and get rid of the ugly undefined.



    if(userAnswer === correctAnswer){ // a good example of why, contrary to felgall's belief, === can cause problems. 42 is not the same as "42", so you must define the answer as a string (which indeed you have done).

    You need to look at the calculation of the scores.

    The closing code tag is /CODE

    Randomizing the questions means that the same question may (and will) appear two or more times. You should instead shuffle the questions into a random order, and then present them sequentially.

    I expect you realise that anyone can see the correct answers simply by using View Source.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 01-22-2014 at 12:07 PM. Reason: typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nsqui5 (01-23-2014)

  • #3
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Thumbs up Thanks

    Thanks for the reply, but what do you mean by the ugly undefined?

  • #4
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    5
    Thanks
    6
    Thanked 0 Times in 0 Posts
    And also what would you recommend other than using "(userAnswer === correctAnswer)"?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by nsqui5 View Post
    Thanks for the reply, but what do you mean by the ugly undefined?
    userAnswer = prompt(question).toLowerCase();

    shows undefined in the prompt box. Change it to

    userAnswer = prompt(question,"").toLowerCase();

    OK?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nsqui5 (01-26-2014)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by nsqui5 View Post
    And also what would you recommend other than using "(userAnswer === correctAnswer)"?
    if(userAnswer === correctAnswer){ is quite OK here, but only because you defined correctAnswer = "42", that is a string rather than a number. I prefer == which would perform the type conversion.. But I have to say that felgall disagrees.

    var x = 42; // number
    var y = "42" // string
    if (x=== y) // false
    if (x==y) // true

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    nsqui5 (01-25-2014)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •