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 JavaScript Basics (Retired) Making Decisions with Conditional Statements The Conditional Challenge

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

Why my code is not working?

var correct = 0;

var string_ques1 = prompt("How many Continents are there in the world?");
var ques2 = prompt("Largest continent in the world?");
var string_ques3 = prompt("How many Oceans are there in the world?");
var ques4 = prompt("What is the capital of Australia?");
var ques5 = prompt("Which is the longest river in the world");

ques1 = parseInt(string_ques1);
ques3 = parseInt(string_ques3);

if (ques1 ==== 7) {
    correct +=1
}
if (ques2.toUpperCase ==== 'ASIA') {
    correct +=1
}
if (ques3 ==== 5) {
    correct +=1
}
if (ques4.toUpperCase ==== 'CANBERRA') {
    correct +=1
}
if (ques5.toUpperCase ==== 'NILE') {
    correct +=1
}

document.write("<p>You got " + correct + " out of 5 questions correct.</p>")


if (correct === 5) {
    document.write("Gold Crown");
} else if (correct >= 3) {
    document.write("Silver Crown");
} else if (correct >= 1) {
    document.write("Bronze Crown");
} else {
    document.write("duh !You are good for nothing.");
}    

What am I doing wrong here?

Or is it necessary to add IF statements when I defined the variables??

8 Answers

Richard Hall
Richard Hall
9,139 Points

Oh ha, you forgot () at the end. It needs to be toUpperCase()

Hi Tushar, in your if statements, you add the condition ==== with 4 ='s instead of just 3. ===

If you look at the console at the bottom it shows you which line of code has the bug.

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

Oh thanks a silly mistake! but now there is another problem, When I execute this code; even if i answer all the question correct , it still says 2/5 correct!

Why is that? :O

Richard Hall
Richard Hall
9,139 Points

The second problem with your code is inconsistent variable names. You have:

  • var string_ques1
  • var ques2
  • var string_ques3
  • var ques4
  • var ques5

While below you are calling them in your if statements "ques1", "ques2", etc. remove the "string_" from 1 and 3 and that should help you with your problem.

Richard Hall
Richard Hall
9,139 Points

whoops, missed your section:

ques1 = parseInt(string_ques1); ques3 = parseInt(string_ques3);

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

But I changed that parseInt()

And I can name whatever i want the variables? How can naming the variable affect my code?

for just simplicity so that i could just use ques1, ques1, etc ,etc so that's why I used string_ques1 and then changed the input to the integer stored in 'ques1'

Tushar Singh
PLUS
Tushar Singh
Courses Plus Student 8,692 Points

OOh ok...now why is not working then..it says only 2 questions are correct even though I am answering each and every question correct?

It seems like string answers are not working properly! answers with the integers are fine...

var correct = 0;

var string_ques1 = prompt("How many Continents are there in the world?");
var ques2 = prompt("Largest continent in the world?");
var string_ques3 = prompt("How many Oceans are there in the world?");
var ques4 = prompt("What is the capital of Australia?");
var ques5 = prompt("Which is the longest river in the world");

ques1 = parseInt(string_ques1);
ques3 = parseInt(string_ques3);

if (ques1 === 7) {
    correct +=1
}
if (ques2.toUpperCase === 'ASIA') {
    correct +=1
}
if (ques3 === 5) {
    correct +=1
}
if (ques4.toUpperCase === 'CANBERRA') {
    correct +=1
}
if (ques5.toUpperCase === 'NILE') {
    correct +=1
}

document.write("<p>You got " + correct + " out of 5 questions correct.</p>");


if (correct === 5) {
    document.write("Gold Crown");
} else if (correct >= 3) {
    document.write("Silver Crown");
} else if (correct >= 1) {
    document.write("Bronze Crown");
} else {
    document.write("duh !You are good for nothing.");
}    
Richard Hall
Richard Hall
9,139 Points

The ".toUpperCase" are causing an issue with the responses. I removed them and got 5/5

Tushar Singh
Tushar Singh
Courses Plus Student 8,692 Points

And why is it casing an issue??

Dosen't make any sense, I mean i get the input from the user, convert alphabets into upper case and then equate it to the upper case answer..

It should work fine :/

Richard Hall
Richard Hall
9,139 Points

You're using it correctly as far as I can tell. I'm looking into why it's not working :/