HELP WITH CONDITIONERS (IF ELSE)

1

I am learning JS online and they ask me to do a program where the following is done:

  • Ask at least five questions
  • Maintain a record of the number of questions the user answered correctly
  • Provide a final message after the test so that the user knows the number of questions answered correctly.
  • Classify the player. If the player answered all five correctly, give the gold crown to that player: 3-4 is a silver crown; 1-2 correct answers is a bronze crown and 0 correct is no crown.
  • For some strange reason which I still do not identify, I only get 2 correct answers. Why is not it still adding to the variable of correct answers? What do I need to modify?

    var question1 = prompt("2 + 2 = ?");
    var userAnswer1 = parseInt(question1);
    
    var question2 = prompt("Capital de Morelos");
    var userAnswer2 = question2.toUpperCase;
    
    var question3 = prompt("Primer dia de la semana");
    var userAnswer3 = question3.toUpperCase;
    
    var question4 = prompt("Cuanto vale PI");
    var userAnswer4 = parseInt(question4);
    
    var question5 = prompt("8 * 3 = ?");
    var userAnswer5 = parseInt(question5);
    
    var userAnswerCorrect = 0;
    
    if( userAnswer1 === 4) {
      userAnswerCorrect ++;
    }
    
    if( userAnswer2 === "CUERNAVACA" || userAnswer2 === "CUERNA") {
      userAnswerCorrect ++;
    }
    
    if( userAnswer3 === "LUNES") {
      userAnswerCorrect ++;
    }
    
    if( userAnswer4 === 3.14 || userAnswer4 === 3.1416) {
      userAnswerCorrect ++;
    }
    
    if( userAnswer5 === 24 ) {
      userAnswerCorrect ++;
    }
    
    document.write("<p>You got " + userAnswerCorrect + " answers correct!");
    
    if( userAnswerCorrect === 5) {
      document.write("<p>You got a GOLD CROWN!</p>");
    } else if( userAnswerCorrect === 4 || userAnswerCorrect === 3 ) {
      document.write("<p>You got a SILVER CROWN!</p>");
    } else {
      document.write("<p>You got a BRONZE CROWN!</p>");
    }
    
        
    asked by Arturo Pérez 03.11.2018 в 00:48
    source

    1 answer

    3

    The problem is in your answers 2 and 3. You are trying to pass them to uppercase but they are not really calling the method:

    var question2 = prompt("Capital de Morelos");
    var userAnswer2 = question2.toUpperCase;
    
    var question3 = prompt("Primer dia de la semana");
    var userAnswer3 = question3.toUpperCase;
    

    The right thing should be:

    var question2 = prompt("Capital de Morelos");
    var userAnswer2 = question2.toUpperCase();
    
    var question3 = prompt("Primer dia de la semana");
    var userAnswer3 = question3.toUpperCase();
    

    Notice how I call the toUpperCase() method using the parentheses. That's why when comparing it did not give you the expected result:

    > var respuesta = prompt("Nombre: ");
    > respuesta
    "César"
    > respuesta.toUpperCase
    ƒ toUpperCase() { [native code] }
    > respuesta.toUpperCase == "CÉSAR"
    false
    > respuesta.toUpperCase()
    "CÉSAR"
    > respuesta.toUpperCase() == "CÉSAR"
    true
    

    The second error is that you are converting the result of PI to an integer:

    var question4 = prompt("Cuanto vale PI");
    var userAnswer4 = parseInt(question4);
    

    And the solution is very simple. You just have to use parseFloat instead of parseInt :

    > var question4 = prompt("Cuanto vale PI");
    > question4
    "3.1416"
    > parseInt(question4)
    3
    > parseFloat(question4)
    3.1416
    > var userAnswer4 = parseFloat(question4)
    > userAnswer4 === 3.14 || userAnswer4 === 3.1416
    true
    
        
    answered by 03.11.2018 / 00:54
    source