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

Kendall McCall
Kendall McCall
6,911 Points

My code is not working as in not prompting the questions at all can anyone help?

/* 
  1. Store correct answers
   - When quiz begins, no answers are correct
*/
let correct = 0;

// 2. Store the rank of a player

let rank = '';

// 3. Select the <main> HTML element

const main = document.querySelector( 'main' )

/*
  4. Ask at least 5 questions
   - Store each answer in a variable
   - Keep track of the number of correct answers
*/
const answer1 = prompt("Whats Kobe'slast name?");
if ( answer1.toUpperCase() === 'Bryant' ) {
  correct += 1;
}
const answer2 = prompt("Whats 10 + 2?");
if ( answer2.toUpperCase() === '12' ) {
  correct += 1;
}
const answer3 = prompt("What state are you in?");
if ( answer3.toUpperCase() === 'North Carolina' ) {
  correct += 1;
}
const answer4 = prompt("How old is Mamba?");
if ( answer4.toUpperCase() === '1' ) {
  correct += 1;
}
const answer5 = prompt("When is Kendall's birthday?");
if ( answer5.toUpperCase() === 'FEBRURARY 20' ) {
  correct += 1;
}
/*
  5. Rank player based on number of correct answers
   - 5 correct = Gold
   - 3-4 correct = Silver
   - 1-2 correct = Bronze
   - 0 correct = No crown
*/

if ( correct === 5 ) {
  rank = "Gold";
} else if ( correct >= 3 ) {
  rank = "Silver";
} else if ( correct >= 1 ) {
  rank = "Bronze";
} else {
  rank = "None :(";
}


// 6. Output results to the <main> element

main.innerHTML = 
  <h2>You got ${correct} out of 5 questions correct. </h2>
  <p>Crown earned: <strong>${rank}</strong></p>
  ;

Moderator edit: Added markdown to post so the code is readable. Please use markdown when posting code to the forum so correct and readable formatting is achieved.*

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 89,653 Points

Hi Kendall,

This is where we need to get into some debugging to look into the code and find out what error messages are coming up in the console. Once we find one breaking change the rest of the program stops working which is why nothing seems to be happening at all.

Can I ask which web browser you're using and if you know how to interpret the JavaScript console and analyse console error messages?

Don't worry if not. If we can see a forked workspace hopefully we can analyse your code there and help you out. :-)

3 Answers

Zachary Cipolletta
Zachary Cipolletta
2,868 Points

The issue is at the very end of your code:

main.innerHTML = <-- you just need to add a backtick ( ` ) here

<h2>You got ${correct} out of 5 questions correct. </h2> <p>Crown earned: <strong>${rank}</strong></p> <-- and another backtick ( ` ) here

The backticks tell the compiler or browser that this whole thing is a template literal. Once you encase those last couple lines in backticks everything runs perfectly after that!

Kendall McCall
Kendall McCall
6,911 Points

Thanks! It's always the small things that get me smh lol

Kendall McCall
Kendall McCall
6,911 Points

Im using google chrome as my browser and the error I'm getting is "Uncaught SyntaxError: Unexpected token '<'" and it's saying error is on line 62 but I don't see the problem

Laura Owens
Laura Owens
15,044 Points

In your if statements:

  1. To compare a STRING variable --> use .toUpperCase() AND MAKE the comparison/correct answer UPPERCASE too. (Minor source of frustration for me.)

2. To compare a NUMBER variable --> use + in front of your entered variable. In your case, I think this would work better than trying to uppercase a number:

const answer2 = prompt("Whats 10 + 2?"); => const answer2 = +prompt("Whats 10 + 2?"); //Converts answer2 to a # if ( answer2.toUpperCase() === '12' ) { => if ( answer2 === 12) { // Comparing 2 numbers, not strings that are #s correct += 1;

}

I think you could probably just add a + in the if statement in front of "answer2" and remove "toUpperCase() and the quotes around the number 12.

I'm new too, so these things may not be mandatory for the language. There are always lots of ways to accomplish the same thing. But, logically, it may be better programming practice in the long run to use numbers as numbers and strings as strings. (No criticism. Individuality rules. :)