Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics Making Decisions in Your Code with Conditional Statements The Conditional Challenge

Conditional Challenge Solution (problems)

Hey there,

I'm doing the conditional challenge in JavaScript basics, and I created this code, but no matter how many questions I get correct (tried everything 0-5) it always returns the command for 'silver' ranking. (Also, I'm aware I didn't create a rank and perhaps that's where I am going wrong, it seemed redundant at the time, but perhaps that is the main issue...). At any rate, here is my code, I would really appreciate any feedback on what I've written! I am very new and looking to learn as much as I can!

var correctAnswers = '';

var questionOne = prompt("What is the capital of France?").toUpperCase;
var questionTwo = prompt("What is the shortest month of the year?").toUpperCase;
var questionThree = prompt("What animal is 'The King of the Jungle'?").toUpperCase;
var questionFour = prompt("Which direction does the sun rise?").toUpperCase;
var questionFive = prompt("Which planet is 'The Red Planet'?").toUpperCase;

var answerOne = 'PARIS';
var answerTwo = 'FEBRUARY';
var answerThree = 'LION';
var answerFour = 'EAST';
var answerFive = 'MARS';


if (questionOne === answerOne) {
  correctAnswers += 1;
}

if (questionTwo === answerTwo) {
  correctAnswers += 1;
}

if (questionThree === answerThree) {
  correctAnswers += 1;
}

if (questionFour === answerFour) {
  correctAnswers += 1;
}

if (questionFive === answerFive) {
  correctAnswers += 1;
}


if (correctAnswers >=5) {
  document.querySelector('main').innerHTML = "You got all 5 questions right! You are so clever. Here is a gold crown.";
} else if (correctAnswers === 3 || 4) {
  document.querySelector('main').innerHTML = "You got a few questions right. Here is a silver crown.";
} else if (correctAnswers === 1 || 2) {
  document.querySelector('main').innerHTML = "You got a some right. Here is a bronze crown.";
} else {
  document.querySelector('main').innerHTML = "You didn't get any questions correct. No crown for you.";
}

1 Answer

Nils Kriedner
Nils Kriedner
32,688 Points

Hi Chelsea,

first of all: great job for a newbie! :-)

Spontaneously I see two issues...

The first thing is that you declare the variable correctAnswers to an empty STRING, but you want it to be a number, right? So you should just assign a 0 to it when you define it.

Second thing is that you used toUpperCase without CALLING it. To call it you have to add () at the end, like this: exampleString.toUpperCase();

If you do the changes, everything should work fine (just tried it out). If not, let me know and I'll help again! :-)

Happy coding and blessings from Berlin, Nils

PS: If my answer helped you, you can upvote it, if it solved your question you can mark it as "best answer", so it is clear to everyone that your question is answered.

Hi Nils!

Thanks so much for your reply! You are absolutely right that I had those two things wrong! What I also discovered is that my two "else if" statements were incorrect! I had:

else if (correctAnswers === 1 || 2)

when I should have had

else if (correctAnswers === 1 || correctAnswers === 2)

With those updates my program was indeed running!

Thanks again for your help!

Happy coding :)