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) Working With Numbers Numbers and Strings

Chris Wade
Chris Wade
9,574 Points

Can you use written numbers with whole numbers to return a total value?

While its easy to think that the user will enter the numbers we are asking for as whole numbers or decimals what if they type the word out? As it is now it will return only NaN but for real world use if a user got this error it would be confusing and frustrating. Is there anyway to assign each number with a its value as a word? Would you have to do this for every number? Or is there some kind of shorthand or maybe formula you can apply to make the code read numbers written as words as numbers?

Daniel Box
Daniel Box
1,939 Points

Better to have some user side validation which clearly tells the user that the value entered needs to be a number should they try to type in text.

1 Answer

Corey Slaven
Corey Slaven
13,997 Points

I agree with Daniel on this one. It is better to keep things simple by using client side validation than to use up a bunch of time writing an algorithm that makes it possible to accept numbers as words and integers. https://en.wikipedia.org/wiki/KISS_principle