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 Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1

Alan Sea
Alan Sea
6,781 Points

Confusion re: toUpperCase()

https://w.trhou.se/prcpugy8ad

I'm confused. My array and my prompts work fine (except for the minor spelling errors that I noticed since I took the screenshot), but I can't seem to get the expected result from my toUpperCase() call. I'm trying to change the user's guess so that it matches the value in the correct position of the array regardless of how they type it into the prompt. I've tried calling it on both guess and answer inside and outside of the if statement. What am I missing?

3 Answers

toUpperCase()....makes all letters to UPPERCASE but in your if statement your condition was ... uhm i mean the value of answer was NOT uppercase. Try to make all letters of the answer also to uppercase...since you made all letters of the variable guess to uppercase

 if (guess === answer.toUpperCase()) { //you made guess already toUpperCase in your code
    alert('Right! ' + guess +' is the correct answer. Keep going!');
    totalCorrect++;

  }
Alan Sea
Alan Sea
6,781 Points

So it wouldn't check for an uppercase version of the answer just the same even though the values of the answers as they are stored in the array already begin with an uppercase letter?

toUpperCase() makes ALL letters of the string to uppercase...not just the first ;) thats why we can make a good condition with it. It is not important if the user writes blue, bLue, bluE...everything gets converted to BLUE

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

I think the problem is that you've only added .toUpperCase() to your guess variable - you should add it to the answer too..

var guess = prompt(quizQuestions[i][0]).toUpperCase();
var answer = quizQuestions[i][1].toUpperCase();

or

if (guess.toUpperCase() === answer.toUpperCase())

Not sure if both examples will work or just one of them, but I hope this will help you :-)

Alan Sea
Alan Sea
6,781 Points

I thought there'd be no need to call the method on both because the answers in the array already begin with an uppercase letter…?

Henrik Christensen
seal-mask
.a{fill-rule:evenodd;}techdegree
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points

If you don't want to use .toUpperCase() on the answer variable then you need to change your answers in your array to all uppercase letters ;-)

The only reason why we do this toUpperCase() (and other kind of) stuff here is to prevent mistakes...mistakes made by the users. It would be annoying if a user writes "green" (which would be a "correct" answer...but not to the program) and gets the wrong answer by the program. (and then writes amail to you because he/she is angry ... and then calls you ... (typical day of the support :D))

Alan Sea
Alan Sea
6,781 Points

As a follow-up, how would you tackle a large array where you may have some answers that are capitalized (at index[0]) but others that are not. How could you write the print statement in such a way where all answers would output the desired capitalization without having to go back through the array and change the inconsistencies. Is there a method call for that?

There is the Class WordUtils from apache commons-lang. You would need to download commons.appache.org and import org.apache.commons.lang3.text.WordUtils

to your java file (also you need that package in your project). http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html

which can make every first letter of a word to uppercase (well the class has more of methods not just one of course).

Apache Commons Lang has some methods which are not part of the java.API.lang yet http://commons.apache.org/proper/commons-lang/

If you are new to java this might be a little bit confusing...but you might get in touch with Commons Lang later. Edit: I like treehouse...but formating the posts is rly frustraiting grrrr