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 Solution

Sebastian Eguez
Sebastian Eguez
8,248 Points

Answer w/ a space = WRONG

// quiz begins, no answers correct
var correct = 0;

// ask questions
var answer1 = prompt("Name a programming language that's algo a gem.");
if (answer1.toUpperCase() === "RUBY") {
   correct += 1; 
}
var answer2 = prompt("Name a programming language that's algo a snake.");
if (answer2.toUpperCase() === "PYTHON") {
   correct += 1; 
}
var answer3 = prompt("What language do you use to style web pages?");
if (answer3.toUpperCase() === "CSS") {
   correct += 1; 
}
var answer4 = prompt("What language do you use to build the structure of web pages?");
if (answer4.toUpperCase() === "HTML") {
   correct += 1; 
}
var answer5 = prompt("What 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>No crown for you. You need to study.</strong></p>");
}

For the first question (though this applies to all of them):

If I answer "ruby " with a space (" ") after "ruby", it will be wrong.

How can I fix this?

Thank you!

2 Answers

John Coolidge
John Coolidge
12,614 Points

Hello Sebastian,

You can use the JavaScript method .trim() to remove excess whitespace from around a string. So, if someone answers " ruby ", you should be able to do the following:

answer = prompt("My question here!").trim()

When they enter " ruby ", the .trim() method removes the whitespace, leaving you with:

answer = "  ruby  ";

answer.trim();

console.log(answer); // "ruby" instead of "  ruby  "

When the .trim() method is used, any string that the user puts into the prompt will have any extraneous whitespace removed and then stored into the variable "answer".

I hope this helps.

John

I'm not 100% sure of what you mean, but a space is a character, so in your conditional statement if there is no space after the word ruby, then in the prompt if you add a space it will return false not true.