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

Elisa Burghard
Elisa Burghard
9,276 Points

Combining two methods - Build A Quiz Challenge, Part I

I was using questions that needed strings as answers. So I used the .toUpperCase() method.

var answer = prompt (questions[i][0]);
answer = answer.toUpperCase();

Dave had numbers as answers and therefore used parseInt(). Is there a way to use both these methods(at once) in case you have both strings and numbers as answers to different questions?

3 Answers

rydavim
rydavim
18,813 Points

If I'm understanding your question correctly then the short answer is no, you can't use them both at once.

parseInt() will return the first integer encountered in a string, ignoring the rest of the string. If it encounters a character that is not a number, it will return NAN.

If you wanted to combine them, there are a couple ways you could do it. You could try parseInt and then test if it returned NAN and, if so, follow up with toUpperCase. You could search the string for numbers and pull them out into their own variables and parse everything separately. There are probably even better ways out there to handle this problem.

Hopefully that helps clarify things, but let me know if you still have questions. Happy coding!

Elisa Burghard
Elisa Burghard
9,276 Points

Thank you! I don't know if I remember correctly, but I think in Python it is possible to combine multiple methods. Is that ever possible in JavaScript, just not in this case?

rydavim
rydavim
18,813 Points

Elisa Burghard - yes, stringing multiple methods together is actually incredibly common in JavaScript. It's called method chaining, and you'll do it all the time. It's just that these particular two methods aren't really compatible out of the box due to type differences.

This particular article is using jQuery, so the syntax will look a bit different than with vanilla JS, but I think he does a good job of explaining the principles and such: Understanding Method Chaining in Javascript

Elisa Burghard
Elisa Burghard
9,276 Points

Thank you so much, I am excited to get to know JavaScript better! I tried various times to link you, how did you do that? neither https://teamtreehouse.com/@[rydavim](https://teamtreehouse.com/rydavim)/ nor @rydavim worked...

rydavim
rydavim
18,813 Points

Hmm, sorry I’m not sure. Usually doing @ and then immediately starting to type a name will pop-up a list of people to link to. Selecting from that list should automatically link to that user. Your mention did still send a notification to me, separate from the reply notification, so maybe it’s just a display issue?

Anyway, keep it up - JavaScript is a ton of fun!

Elisa Burghard
Elisa Burghard
9,276 Points

rydavim there you are! Maybe it was a temporary problem in my browser. Thanks again, and yes, it´s so much fun!