Frontend Development

JavaScript code help

Submitted by Krave, , Thread ID: 19257

Thread Closed
09-03-2016, 07:24 PM
#1
Title pretty much sums up why I'm here. I'm currently teaching myself JavaScript and as a test to myself, I thought I'd code a small quiz but when I answer the quiz's questions correctly, I still end up with 0. I've posted the code below and if anyone can help me out, that's be awesome!

Code:
// start quiz, none are correct
var correct = 0;

// questions
var answer1 = prompt("Name a programming language that's also named after a gem");
if (answer1.toUpperCase === 'RUBY') {
correct += 1;
}

var answer2 = prompt("Name a programming language that's also a snake");
if (answer2.toUpperCase === 'PYTHON') {
correct += 1;
}

var answer3 = prompt("Name the language that styles web pages");
if (answer3.toUpperCase === 'CSS') {
correct += 1;
}

var answer4 = prompt("Name the programming language that adds function to a web page");
if (answer4.toUpperCase === 'JAVASCRIPT' || answer4.toUpperCase === 'JS') {
correct += 1;
}

var answer5 = prompt("What is the language that adds structure to a web page?");
if (answer5.toUpperCase === 'HTML') {
correct += 1;
}

// output score
if (correct === 5) {
document.write("<p>You got " + correct + " out of 5 correct and earned the gold medal!</p>");
} else if (correct > 3) {
document.write("<p>You got " + correct + " out of 5 correct and earned the silver medal!</p>");
} else if (correct > 1) {
document.write("<p>You got " + correct + " out of 5 correct and earned the bronze medal!</p>");
} else {
document.write("<p>You got " + correct + " out of 5 correct. You need to study more.</p>");
}

RE: JavaScript code help

#2
You are trying to use .toUpperCase like a variable, it's actually a function

Code:
// start quiz, none are correct
var correct = 0;

// questions
var answer1 = prompt("Name a programming language that's also named after a gem");
if (answer1.toUpperCase() === 'RUBY') {
correct += 1;
}

var answer2 = prompt("Name a programming language that's also a snake");
if (answer2.toUpperCase() === 'PYTHON') {
correct += 1;
}

var answer3 = prompt("Name the language that styles web pages");
if (answer3.toUpperCase() === 'CSS') {
correct += 1;
}

var answer4 = prompt("Name the programming language that adds function to a web page");
if (answer4.toUpperCase() === 'JAVASCRIPT' || answer4.toUpperCase() === 'JS') {
correct += 1;
}

var answer5 = prompt("What is the language that adds structure to a web page?");
if (answer5.toUpperCase() === 'HTML') {
correct += 1;
}

// output score
if (correct === 5) {
document.write("<p>You got " + correct + " out of 5 correct and earned the gold medal!</p>");
} else if (correct > 3) {
document.write("<p>You got " + correct + " out of 5 correct and earned the silver medal!</p>");
} else if (correct > 1) {
document.write("<p>You got " + correct + " out of 5 correct and earned the bronze medal!</p>");
} else {
document.write("<p>You got " + correct + " out of 5 correct. You need to study more.</p>");
}

Users browsing this thread: 3 Guest(s)