I have been trying to learn JavaScript for the past few days and decided it was time to put what I had learnt into practice. Here is the program I created. It's a five-question quiz which counts the number of correct questions as you go through the quiz and also ranks you at the end of the program.
//I am going to place a variable here to make initialise the number of correct values at 0
var correct = 0;
// ask questions prompts
var answer1 = prompt("Name a Programming language thats also a gem");
if ( answer1.toUpperCase() === 'RUBY') {
correct += 1;
}
var answer2 = prompt("Name a Programming language thats also a snake");
if ( answer2.toUpperCase() === 'PYTHON') {
correct += 1;
}
var answer3 = prompt("What language do we use to style webpages?");
if ( answer3.toUpperCase() === 'CSS' || answer3.toUpperCase() === 'CASCADING STYLE SHEETS') {
correct += 1;
}
var answer4 = prompt("What language do you use to build the structure of a web page?");
if ( answer4.toUpperCase() === 'HTML' || answer4.toUpperCase() === 'HYPER TEXT MARKUP LANGUAGE') {
correct += 1;
}
var answer5 = prompt("What core language do you use to add interactivity to a web page");
if ( answer5.toUpperCase() === 'JAVASCRIPT') {
correct += 1;
}
// OUTPUT RESULTS
document.write("<p>You got " + correct + " out of 5 questions correct.</p>");
//output rank
if ( correct === 5 ) {
document.write("<p><strong>You earned a gold crown!</strong></p>");
} else if ( correct >= 3) {
document.write("<p><strong>You earned a silver crown.</strong></p>");
} else if ( correct >= 1) {
document.write("<p><strong>You earned a bronze crown.</strong></p>");
} else {
document.write("<p><strong>Sorry, no crown for you. Go home and study. n </strong></p>");
}
Have I coded the program with what is considered best practices, and if not, what could I do to improve it?