Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

.toUpperCase is given me an error but not everytime.

Under the question 2 note. The code runs if I use 'else if' statements. but if I take away the 'else if' statement and just use 'else' like in question one, Question 2 sends me an "error : Uncaught TypeError: Cannot read property 'toUpperCase' of undefined " after attemtping to answer. IT works fine with if else as shown below... not sure what I 'm doing wrong or if it needs to be this way:

var correctAnswer = false;
var score = 0;



//question1

var question1 = prompt('What is 10 * 10?');

if (parseFloat(question1) === 100){
      correctAnswer = true;
      alert('That is correct the answer!');
  score += 10;

  }

      else  {
            correctAnswer = false;
            var question1Retry = prompt( question1 +' is incorrect please try again. What is 100 * 100?');
}


if (parseFloat(question1Retry) === 100){
          correctAnswer = true;

          alert('That is correct the answer!');
        score += 5;


      }
          else{

          correctAnswer = false;

                }
//------question 2

var question2 = prompt('What programming language is named after a jewel?');

if (question2.toUpperCase() === 'RUBY'){

  correctAnswer = true;
  alert('That is correct the answer!');
    score += 10;

  }

    else if (question2.toUpperCase() !== 'RUBY') {

          correctAnswer = false;
        var question2Retry = prompt(question2 + ' is not correct, please try again. What programming language is named after a jewel?');

    }

  else if (question2Retry.toUpperCase() === 'RUBY'){

                correctAnswer = true;
                alert('That is correct the answer!');
                score += 5;

        }
         else {

         correctAnswer= false;
         }

1 Answer

Hey Auroiah,

I think your problem is that in that second else if statement, you're referencing question2retry as a variable, but that variable only gets declared in the previous else if block:

else if (question2.toUpperCase() !== 'RUBY') {
    correctAnswer = false;
    var question2Retry = prompt(question2 + ' is not correct, please try again. What programming language is named after a jewel?');
} else if (question2Retry.toUpperCase() === 'RUBY'){
    correctAnswer = true;
    alert('That is correct the answer!');
    score += 5;
}

Remember that with multiple else if statements, only one condition can evaluate to true. That means that if your first else if statement evaluates to true, the second else if never runs - which is fine. But if your first else if is false, the code jumps to the next else if block, where you're using 'question2Retry' in the conditional block without ever having declared or instantiated it - in this scenario, 'var question2Retry = ...' from the first else if block has never executed. The reason else works and not else if is because else doesn't care about the conditions you're passing in - it's a black and white alternative to the 'falsiness' of the previous else if.

In short, the system is quite right in telling you that question2Retry is undefined in that second else if block. You have a couple of options - you could declare it before the conditionals (and assign it an empty string), and use the conditional statements to set its value accordingly, or you could simply stick with your else statement.

Hope that helps!

Thank you!