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 trialvwarner
1,583 PointsHow can a prompt dialogue box accept either a answer in as a number or string using the if/else conditional.
//This is my example
// 2. Ask user a questions
var questionTwo = prompt("How many wheels does a car has?").toLowerCase();
// 2. Answer of questions
var answerTwo;
if (answerTwo === '4' || answerTwo === 'four'){
answerTwo = true;
}
if (answerTwo === questionTwo){
document.write('<p style="color:green;">You are correct!</p>');
} else {
document.write('<p style="color:red;">That\'s incorrect!</p>');
}
2 Answers
Marcus Parsons
15,719 PointsHey Darren Joseph,
I edited your code so that it would render better. Here's how I did that:
A much better way to get what you need done is to check to see if questionTwo is equal to one of those answers and write the code then and there instead of wasting memory setting an answer variable. You make the code much more efficient that way.
//This is my example
// 2. Ask user a question
var questionTwo = prompt("How many wheels does a car have?").toLowerCase();
// 2. Answer of question
if (questionTwo === '4' || questionTwo === 'four'){
document.write('<p style="color:green;">You are correct!</p>');
}
else {
document.write('<p style="color:red;">That\'s incorrect!</p>');
}
vwarner
1,583 PointsSuper efficient thanks!!!
Marcus Parsons
15,719 PointsAnytime, Darren! =D
Marcus Parsons
15,719 PointsIf you don't have any more questions, you can go ahead and mark my answer as best so we can mark this as solved!