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

I don't understand why this fails on correctGuess.

/*

  1. Store correct answers
    • When quiz begins, no answers are correct */

let correctGuess = 0

// 2. Store the rank of a player let playerRank = ("No crown");

// 3. Select the <main> HTML element document.querySelector = "main";

/*

  1. Ask at least 5 questions
    • Store each answer in a variable
    • Keep track of the number of correct answers */
      const qOne = prompt("Largest city in FL"); if (qOne.toUppercase() === "MIAMI"){ correctGuess += 1; }

const qTwo = prompt("Place where Disneyworld is"); if (qTwo.toUppercase() === "ORLANDO"){ correctGuess += 1; }

const qThree = prompt("Place where Busch Gardens is"); if (qThree.toUppercase() === "TAMPA"){ correctGuess += 1; }

const qFour = prompt("County with Manatee River"); if (qFour.toUppercase() === "MANATEE"){ correctGuess += 1; }

const qFive = prompt("Place where Univ of FL is"); if (qFive.toUppercase() === "GAINESVILLE"){ correctGuess += 1; }

/*

  1. Rank player based on number of correct answers
    • 5 correct = Gold
    • 3-4 correct = Silver
    • 1-2 correct = Bronze
    • 0 correct = No crown */

if (correctGuess === 5){ playerRank = "Gold" } else if (correctGuess >=3){ playerRank = "Silver";
} else if (correctGuess <=2){
playerRank= "Bronze";
} else {
playerRank = "No Crown"; }

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

main.innerHTML = <h2> You got {correctGuess} right. Your rank is {playerRank}</h2>

9 Answers

Steven Parker
Steven Parker
231,275 Points

Can you give us a fresh snapshot? Also, exactly what steps do you use to "run the code"?

I cut and paste the code from the Treehouse Workspace to the console and run it there. Here is a new snapshot.

https://w.trhou.se/30m5a2p7xs

Steven Parker
Steven Parker
231,275 Points

It looks like the final issue is how you run the project. To start a workspace, click the preview button, it's the little icon that looks like an eye in the upper right. This will cause a new page to open in your browser showing the output from your project.

You won't need to "cut and paste" any code, and that should eliminate the error.

// .toUppercase(), the c in case is not capitalized, it should be like this for all if statements above:
.toUpperCase()

// main.innerHTML = <h2> You got {correctGuess} right. Your rank is {playerRank}</h2>, should be like this:
main.innerHTML = `<h2> You got ${correctGuess} right. Your rank is ${playerRank}</h2>`;

// I did not see what the main was referenced to but if you fix the above issues, your code should run fine.
// when I was testing it I had to console.log the correctGuess and playerRank and they were working perfectly fine

Just spotted a flaw in your code:

// it's here > else if (correctGuess <=2 ...
// if you have 0 correctGuesses it will still return the outcome for this statement, in this case, playerRank= "Bronze";
// it will never be able to get to 
else {
    playerRank = "No Crown"; 
}

// I will write it like this, so that if the correctGuess is 0 the playerRank would equal to "No Crown":
else if (correctGuess <=2 && correctGuess > 0) {
    playerRank= "Bronze";
}

I made your suggested changes. TY. I posted the code because it still fails on correctGuess.

https://w.trhou.se/i2p0stb2m9

https://w.trhou.se/i2p0stb2m9

I get an error that correctGuess is already declared and it fails when I run it.

Steven Parker
Steven Parker
231,275 Points

I didn't see that issue in the snapshot, but I did spot several spellings of "toUpperCasease" instead of "toUpperCase".
Also "main" still needs to be defined.

I fixed all of that. Here is the link to the snapshot. Same error. correctGuess already declared. Hope you can figure this out. I'm stumped. Thanks for helping.

https://w.trhou.se/5380pbkz2f

Steven Parker
Steven Parker
231,275 Points

You have "toUppercase" (with lower-case "c") instead of "toUpperCase" (with capital "C") in several places.

Also, it looks like "main" has not been defined, and your template literal tokens are missing the "$" symbol.

And when posting code to the forum, use Markdown formatting to preserve the appearance, or share the entire workspace by making a snapshot and posting the link to it.

Steven Parker
Steven Parker
231,275 Points

UPDATE: There's an issue with the method calling sequence on line 13:

const main = document.querySelector = "main";  // original
const main = document.querySelector("main");   // corrected
// .toUpperCasease(), 4 of your if statements have this when it should be
.toUpperCase()

// your main is not defined, so replace document.querySelector = 'main', with this:
const main = document.querySelector('main');

// this should work now

I fixed all of that. Here is the link to the snapshot. Same error. correctGuess already declared. Hope y can figure this out. I'm stumped. Thanks for helping.

https://w.trhou.se/5380pbkz2f

// const main = document.querySelector = "main"; this is wrong, should be
const main = document.querySelector("main");

TY, Stivan. I corrected that but the same error on correctGuess. Here is the corrected code.

https://w.trhou.se/kqh79m2sh1

// I'm still seeing the 
const main = document.querySelector = ("main");
// did you link the up-to-date code?
// also where are you getting the error from? in the console.log?

I see. The double =. I fixed that but when I run to code in the console, I get this:

VM40:1 Uncaught SyntaxError: Identifier 'correctGuess' has already been declared

Just tested the fresh snapshot and there is nothing wrong with your code. It works perfectly fine.

Thank you so much Steven and Stivan. I had no idea should not cut and paste in the console. The instructor showed us to do that in "Developer Tools".

I did appreciate the guidance on the poorly written code. ;-)