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

prompt isn't working

//Ask 5 questions
var Question1= prompt('What language do they speak in Brazil?');

var Question2= prompt('What language do they speak in Spain besides Spanish?');

var Question3= prompt('What language do they speak in Portugal?');

var Question4= prompt('What language do they speak in Canada besides English?');

var Question5= prompt('What language do they speak in some parts of Haiti?');

//Correct corresponding answers
if (Question1 === 'Portuguese'){var answerOne= 1}
if (Question2 === 'Catalan'){var answerTwo= 1}
if (Question3 === 'Portuguese'){var answerThree=1}
if (Question4 === 'French'){var answerFour=1}
if (Question5 === 'Creole'){var answerFive=1}

Badges
if (answerOne + answerTwo + answerThree + answerFour + answerFive==5 {
    document.write('Congrats, you have earned the Gold Medal!')
  }
if (answerOne + answerTwo + answerThree + answerFour + answerFive==4 || answerOne + answerTwo + answerThree + answerFour + answerFive==3 || answerOne + answerTwo + answerThree + answerFour + answerFive==2 {
    document.write('Congrats, you have earned the Silver Medal!')
  }
if (answerOne + answerTwo + answerThree + answerFour + answerFive==1) {
  document.write('Congrats, you have earned the Bronze Medal!')
}

MODERATOR EDIT: Added Markdown to Post so the code is readable. Please refer to the Markdown Cheatsheet when posting code in the Community for proper markdown.

3 Answers

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Gena, your problem is that there is no value being assigned to each of the answer variables if the user enters an incorrect answer.

At the conditional checks, all of them aren't working because it's trying to add undefined variables together with numbers (if any correct) resulting in NaN ( not a number ).

Easiest way (how you have written your code) would be to assign the answer variable a value of 0 if the user is incorrect when you check each answer.

if (Question1 === 'Portuguese'){
    var answerOne= 1
} else {
    var answerOne = 0;
}
if (Question2 === 'Catalan'){
    var answerTwo= 1
} else {
    var answerTwo = 0;
}
if (Question3 === 'Portuguese'){
    var answerThree=1
} else {
    var answerThree = 0;
}
if (Question4 === 'French'){
    var answerFour=1
} else {
    var answerFour = 0;
}
if (Question5 === 'Creole'){
    var answerFive=1
} else {
    var answerFive = 0;
}

I would recommend you create the variables first with default values, then go through the checks, to make sure they are all defined.

var answerOne = 0;
var answerTwo = 0;
var answerThree = 0;
var answerFour = 0;
var answerFive = 0;

if (Question1 === 'Portuguese'){answerOne= 1}
if (Question2 === 'Catalan'){answerTwo= 1}
if (Question3 === 'Portuguese'){answerThree=1}
if (Question4 === 'French'){answerFour=1}
if (Question5 === 'Creole'){answerFive=1}

[Edit] There are also syntax issues, such as the word Badges, and brackets missing at the end of a few if checks.

sorry where do i put the "var answerOne=0; var answerTwo= 0..... "part of the code

Umesh Ravji
Umesh Ravji
42,386 Points

Here's what the full code would look like:

//Ask 5 questions
var Question1= prompt('What language do they speak in Brazil?');

var Question2= prompt('What language do they speak in Spain besides Spanish?');

var Question3= prompt('What language do they speak in Portugal?');

var Question4= prompt('What language do they speak in Canada besides English?');

var Question5= prompt('What language do they speak in some parts of Haiti?');

var answerOne = 0;
var answerTwo = 0;
var answerThree = 0;
var answerFour = 0;
var answerFive = 0;

if (Question1 === 'Portuguese'){answerOne= 1}
if (Question2 === 'Catalan'){answerTwo= 1}
if (Question3 === 'Portuguese'){answerThree=1}
if (Question4 === 'French'){answerFour=1}
if (Question5 === 'Creole'){answerFive=1}

if (answerOne + answerTwo + answerThree + answerFour + answerFive==5) {
    document.write('Congrats, you have earned the Gold Medal!')
  }
if (answerOne + answerTwo + answerThree + answerFour + answerFive==4 || answerOne + answerTwo + answerThree + answerFour + answerFive==3 || answerOne + answerTwo + answerThree + answerFour + answerFive==2) {
    document.write('Congrats, you have earned the Silver Medal!')
  }
if (answerOne + answerTwo + answerThree + answerFour + answerFive==1) {
  document.write('Congrats, you have earned the Bronze Medal!')
}

it is still not prompting //Ask 5 questions var Question1= prompt('What language do they speak in Brazil?');

var Question2= prompt('What language do they speak in Spain besides Spanish?');

var Question3= prompt('What language do they speak in Portugal?');

var Question4= prompt('What language do they speak in Canada besides English?');

var Question5= prompt('What language do they speak in some parts of Haiti?');

var answerOne = 0; var answerTwo = 0; var answerThree = 0; var answerFour = 0; var answerFive = 0;

//Correct corresponding answers if (Question1 === 'Portuguese'){answerOne= 1} if (Question2 === 'Catalan'){answerTwo= 1} if (Question3 === 'Portuguese'){answerThree=1} if (Question4 === 'French'){answerFour=1} if (Question5 === 'Creole'){answerFive=1}

//Badges if (answerOne + answerTwo + answerThree + answerFour + answerFive==5 { document.write('Congrats, you have earned the Gold Medal!') } if (answerOne + answerTwo + answerThree + answerFour + answerFive==4 || answerOne + answerTwo + answerThree + answerFour + answerFive==3 || answerOne + answerTwo + answerThree + answerFour + answerFive==2 { document.write('Congrats, you have earned the Silver Medal!') } if (answerOne + answerTwo + answerThree + answerFour + answerFive==1) { document.write('Congrats, you have earned the Bronze Medal!') }

Umesh Ravji
Umesh Ravji
42,386 Points

Hi there, you are missing closing brackets on 2 of the if conditional checks:

//Badges
if (answerOne + answerTwo + answerThree + answerFour + answerFive==5 { // missing )
 document.write('Congrats, you have earned the Gold Medal!') 
}
if (answerOne + answerTwo + answerThree + answerFour + answerFive==4 || answerOne + answerTwo + answerThree + answerFour + answerFive==3 || answerOne + answerTwo + answerThree + answerFour + answerFive==2 { //missing ) 
document.write('Congrats, you have earned the Silver Medal!') 
}
if (answerOne + answerTwo + answerThree + answerFour + answerFive==1) {
document.write('Congrats, you have earned the Bronze Medal!')
}