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 trialkabirdas
1,976 Pointswhat 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
Viktor Karatov
4,087 Pointsuse str.toLowerCase() === 'ruby' You'll always have it in lower case no matter what user has entered.
Alexander La Bianca
15,959 PointsThere 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
kabirdas
1,976 PointsThank you
Rex Orioko
3,234 PointsTheo 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
Theo VOGLIMACCI
8,027 PointsTheo VOGLIMACCI
8,027 PointsHello,
don't work, how should i code it?