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 trialjohn larson
16,594 PointstoLowerCase: one works, one doesn't
example one does not work, example 2 does. The look to me like they should both work. Any insight?
//example 1
response = prompt(question.toLowerCase())
//example 2
response = response.toLowerCase()
john larson
16,594 Pointsvar questions = [
["#f00?", "red"],
["#0f0?", "green"],
["#00f?", "blue"]
];
var correctAnswers = 0;
var question;
var answer;
var response;
var html;
function print(message) {
document.getElementById("print").innerHTML = message;
}
for (var i = 0; i < questions.length; i += 1) {
question = questions[i][0]
answer = questions[i][1]
response = prompt(question.toLowerCase())
response = response.toLowerCase()
if (response === answer) {
correctAnswers += 1;
}
}
html = correctAnswers;
print(html)
john larson
16,594 Pointsquestion = questions[i][0]
john larson
16,594 Pointsand thank you chris for responding
Chris Shaw
26,676 PointsI just tested your code and it is working as expected from an execution standpoint, I feel you are thinking question.toLowerCase()
should be doing something else which won't be the case since the question strings you have are already lowercase and can't be transformed any further.
If you wish to see a change occur, you can use the following questions
array which I've updated to change the lowercase f to uppercase. This change will yield the same result as just having them lowercase already.
var questions = [
["#F00?", "red"],
["#0F0?", "green"],
["#00F?", "blue"]
];
john larson
16,594 PointsI think I see what your saying. I was testing the program by typing in upper case answers in response to the prompts. That is where the code failed w/ example one. So I think your saying example one is linking to the original array and is therefore not having an effect on the response to the prompts?
2 Answers
kevinrambaud
5,734 PointsWell, everything is fine, you were just using the toLowerCase method on the question value on the example 1 whereas in the example 2 you use the method on the response value.
What you were probably trying to do is to compare response value and answer value in lower case.
You can do this simply like this :
if (response.toLowerCase() === answer.toLowerCase()) {
correctAnswers += 1;
}
Todd MacIntyre
12,248 Pointsdouble check your question
object. That may be where the error lies.
john larson
16,594 PointsThanks Todd, I will look at that
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsHi john larson,
What is the value of your
question
variable?