JavaScript Bonus Exercises (Final) - Alternative Solutions

Thought it could be helpful if people wanted to post their alternative solutions to the final Bonus Exercises of the JavaScript Course. :computer:

This is a thread for:

  • Comparing your solutions to others if you completed an exercise entirely differently to the solution. :nerd_face:

  • Addressing contingency issues in the provided solutions :no_entry_sign:

    • Where the solution doesn’t necessarily factor in a every input.

When posting, make sure to address the problem you are posting about. :heart:

1 Like

I’ll go first.

In Exercise 18 - Quiz, the solution does not account for when the user inputs a correct answer into the prompt, but with the incorrect casing.

A simple way to remedy this is to convert both the solution and the user answer to lower case using .toLowerCase() in the if boolean.

Extending that, a better answer could be:

var userAnswer = prompt("What is the name of the capital of Morocco?");
var moroccoCapital = "Rabat";

if (userAnswer.toLowerCase() == moroccoCapital.toLowerCase()) {
    console.log("Exactly!");
} else {
    console.log("Sorry, the answer was " + moroccoCapital);
}
1 Like