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 Introducing Conditional Statements

kabirdas
kabirdas
1,976 Points

what if the user types in "ruby" instead of "Ruby?" how do we account for that?

what would the code be for that? is this covered in later lessons?

4 Answers

use str.toLowerCase() === 'ruby' You'll always have it in lower case no matter what user has entered.

Theo VOGLIMACCI
Theo VOGLIMACCI
8,027 Points

Hello,

var answer = prompt ('What programming language is the name of a gem?');
if( str.toLowerCase(answer) === 'ruby === 'ruby' )ย {
  document.write("<p>That's right!</p>");
} else {
  document.write("<p>That's wrong...</p>");
}

don't work, how should i code it?

There is a way to account for it using conditional statements and utilize the following String methods: charAt() and replace()

Lets say you have the following scenario:

string language = prompt("Enter Ruby");
while(language.chartAt(0) === "r") {                    //a while loop will loop as long the condition between ( ) is true.
    language = prompt("Please use upper case 'R'");
}

In the above example you initially ask the user to enter "Ruby". But if the user enters 'ruby' he/she will be asked again to enter it correctly until it is correct.

A better way to go about it is to include logic to replace the lower case 'r' with an upper case 'R'

string language = prompt("Enter Ruby")
if(language.charAt(0) === "r") {
   language.replace("r", "R");           //replace(stringToReplace, newString) to replace the lower case 'r' with 'R'
}

Hope this helps. http://www.w3schools.com/jsref/jsref_replace.asp - read about the replace method http://www.w3schools.com/jsref/jsref_charat.asp - read about the charAt method

Rex Orioko
Rex Orioko
3,234 Points

Theo VOGLIMACCI your declared variable and the one in your If statement don't match, change the str to answer or answer to str, make sure they are the same and it should work, hope that helps