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

Daniel L.
Daniel L.
10,837 Points

The Conditional Challenge

I'm not sure what I'm doing wrong, my variable score keeps coming back as 1 it won't add anything past the first question.

//Score & Rank:
let score = 0;
let rank;

//Questions:
const q1 = prompt(`In the "Pilot" episode, Leonard and Sheldon are sent by their new neighbor Penny, to retrieve which item from her muscle bound ex-boyfriend?`);

if (q1.toUpperCase() === "Television" || q1.toUpperCase() === "TV") {
  score += 1;
} 

const q2 = prompt(`In episode 2, "The Big Bran Hypothesis", Penny gives Leonard the spare key to her apartment, because she needs him to do her a favor. What does this favor involve?`);

if (q2.toUpperCase() === "Furniture Delivery") {
  score += 1;
}

const q3 = prompt(`In episode 3, "The Fuzzy Boots Corollary", Leonard stops by Penny's apartment and notices another guy there. Disheartened he goes back to his apartment and starts to complain. His friends encourage him to go out with someone more like him. Who does Leonard approach for a date?`);

if (q3.toUpperCase() === "Leslie Winkle") {
  score += 1;
} 

const q4 = prompt(`In episode 4, "The Luminous Fish Effect", Sheldon gets fired from his job after insulting the intelligence of his new boss. Which of these endeavors does Sheldon not attempt while unemployed?`);

if (q4.toUpperCase() === "knitting"){
  score += 1;
}

const q5 = prompt(`In episode 11, "The Pancake Batter Anomaly", Penny comes home from a weekend in Nebraska carrying germs from her sick family. Sheldon ends up sick as a result. Knowing how needy Sheldon can be when he is sick, Leonard, Howard and Raj plan to go see a movie marathon. Which marathon is it?`);

if (q5.toUpperCase() === "Planet of the Apes") {
  score += 1;
}

//Ranking:

if (score === 5){
  rank = "Gold";
} else if (score === 4 || score === 3){
  rank = "Silver";
} else if (score === 2 || score === 1){
  rank = "Bronze";
} else {
  rank = "No Crown";
}

//Test:
console.log(score);
console.log(rank);

I even tried a shorter version and pretty much copied Guil's code from his answer except the querySelector portion because I'm just testing so I thought console.log would be quicker but it still doesn't work:

let correct = 0;
let rank;

const q1 = prompt("1 + 1");
if (q1 === 2){
  correct +=1;
}

const q2 = prompt("2+2");
if (q2 === 4){
  correct += 1;
}

const q3 = prompt("3 + 3")
if (q3 === 6){
  correct += 1;
}

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

console.log(correct);
console.log(rank);

1 Answer

Steven Parker
Steven Parker
231,140 Points

An input converted to upper case can't match anything in mixed case, convert your comparison strings:

if (q2.toUpperCase() === "Furniture Delivery") {  // <-- this will never be true
if (q2.toUpperCase() === "FURNITURE DELIVERY") {  // <-- but this can
Daniel L.
Daniel L.
10,837 Points

Thank you so much, that fixed my code!

Steven Parker
Steven Parker
231,140 Points

Daniel L. — Glad to help. You can mark a question solved by choosing a "best answer".
And happy coding!