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

Vahe Aslanyan
Vahe Aslanyan
1,426 Points

I am not sure what I have done wrong, but I keep getting 4 out of 5, even if I answer 5/5 right. Take a look here:

https://i.imgur.com/h1p9Ddj.png Here is the picture that shows the console log. even if I answer the questions fully wrong, I keep getting 4 out of 5.

/*

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

/* First we will define the fundamental variables*/ let score = 0; let answer_answer_question1 = "TELLINGA COMPUTER WHAT TO DO"; let answer_answer_question2 = "LOOP"; let answer_answer_question3 = "PROGRAMMER"; let answer_answer_question4 = "BUG"; let answer_answer_question5 = "COMMAND";

//Here are the questions that the player should answer. const answer_question1 = prompt("What is computer coding?"); console.log(answer_question1); if (answer_question1.toUpperCase = answer_answer_question1); { score += 1; } const answer_question2 = prompt("What is an action that repeats over and over again?"); console.log(answer_question2); if (answer_question2.toUpperCase = answer_answer_question2); { score += 1; } const answer_question3 = prompt("What is a person who writes code and communcates instructions to a computer?"); console.log(answer_question3); if (answer_question3.toUpperCase = answer_answer_question3) { score += 1; } const answer_question4 = prompt("What is an error, or mistake, in a program that prevents the program from being run correctly called?"); console.log(answer_question4); if (answer_question4.toUpperCase = answer_answer_question4) { score += 1; } const answer_question5 = prompt("What is a single instruction for a computer called?"); console.log(answer_question5); if (answer_question5.toUpperCase === answer_answer_question5) { score += 1; } // 2. Store the rank of a player console.log(score);

if (score === 1 || score === 2) { document.querySelector('main').innerHTML = <p>Good job! You have earned a bronze medal!You have earned ${score} points!</p>;

} else if (score === 3 || score === 4) { document.querySelector('main').innerHTML = <p>Good job! You have earned a silver medal! You have earned ${score} points!</p>;

} else (score === 5); { document.querySelector('main').innerHTML = <p>Good job! You have earned a golden medal! You have earned ${score} points!</p>; } // 3. Select the <main> HTML element /*

  1. Ask at least 5 questions
    • Store each answer in a variable
    • Keep track of the number of correct answers */

/*

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

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

John Johnson
John Johnson
11,481 Points

Looks like you might have a typo in the form of a missed space in this piece of code

let answer_answer_question1 = "TELLINGA COMPUTER WHAT TO DO";

If that isn't the problem, are you getting any console errors?

Also, you can add markup to your questions to make them easier to read. Checkout the Markdown Cheatsheet whenever you're typing up a question, comment or answer (it's between the input box and post comment button).

2 Answers

Hi Vahe!

I had to change a lot of stuff to get this to work:

Copy this:

<!DOCTYPE html>
<html>
<body>

<main></main>

<script>
/*

Store correct answers
When quiz begins, no answers are correct */
/* First we will define the fundamental variables*/
let score = 0;
const correct_answer1 = "TELLING A COMPUTER WHAT TO DO";
const correct_answer2 = "LOOP";
const correct_answer3 = "PROGRAMMER";
const correct_answer4 = "BUG";
const correct_answer5 = "COMMAND";

//Here are the questions that the player should answer.
/*
Ask at least 5 questions
Store each answer in a variable
Keep track of the number of correct answers */
const answer_question1 = prompt("What is computer coding?");
console.log( answer_question1.toUpperCase() );
console.log( correct_answer1 );
if (answer_question1.toUpperCase() === correct_answer1) { score += 1; }
console.log(score);

const answer_question2 = prompt("What is an action that repeats over and over again?"); console.log( answer_question2.toUpperCase() );
console.log( correct_answer2 );
if (answer_question2.toUpperCase() === correct_answer2) { score += 1; }
console.log(score);

const answer_question3 = prompt("What is a person who writes code and communcates instructions to a computer?"); console.log( answer_question3.toUpperCase() );
console.log( correct_answer3 );
if (answer_question3.toUpperCase() === correct_answer3) { score += 1; }
console.log(score);

const answer_question4 = prompt("What is an error, or mistake, in a program that prevents the program from being run correctly called?");
console.log( answer_question4.toUpperCase() );
console.log( correct_answer4 );
if (answer_question4.toUpperCase() === correct_answer4) { score += 1; }
console.log(score);

const answer_question5 = prompt("What is a single instruction for a computer called?"); console.log( answer_question5.toUpperCase() );
console.log( correct_answer5 );
if (answer_question5.toUpperCase() === correct_answer5) { score += 1; }
console.log(score);


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

if (score === 5) {
  rank = "Gold";
} else if (score >=3) {
  rank = "Silver";
} else if (score >= 1) {
  rank = "Bronze";
} else {
  rank = false; // NO CROWN
}


// 3. Output results to the <main> element
const main = document.getElementsByTagName('main')[0];
if (rank) {
  main.innerHTML = `<h1>You got ${score} questions correct, earning you the ${rank} crown!</h1>`;
} else {
  main.innerHTML = `<h1>You got 0 questions correct, so you don't get crowned!</h1>`;
}

</script>

</body>
</html>

And past it here:

https://www.w3schools.com/js/tryit.asp?filename=tryjs_array

Replace all the code in the left pane and run it. You can check the console in the right pane.

Your biggest issues were:

1) toUpperCase is a function/method, so you need to call it with ()

2) You had a couple of semicolons in the wrong place: if (answer_question1.toUpperCase() === correct_answer1); <--- RIGHT HERE

3) You had some comments messing things up

4) You didn't target main and set its innerHTML

I hope that helps.

Stay safe and happy coding!

Sorry, Vahe - I had your name wrong.

I don't know where Jose came from!?!

(Although I do have a gazzillion tabs open and must have gotten confused!?!

-Pete

Vahe Aslanyan
Vahe Aslanyan
1,426 Points

Thank you so much for your answer. After reviewing some old course material, I saw the toUpperCase error. Thank you for your notes, I will apply them for the next course.