7 kyu

String to integer conversion

2,055 of 4,480Uraza

Description:

JavaScript provides a built-in parseInt method.

It can be used like this:

  • "10" returns 10
  • "10 apples" also returns 10

We would like it to return "NaN" (as a string) for the second case because the input string is not a valid number.

You are asked to write a function with the following rules:

  • It should make the conversion if the given string only contains a single integer value (and possibly spaces - including tabs, line feeds... - at both ends)
  • For all other strings (including the ones representing float values), it should return NaN
  • It should assume that all numbers are not signed and written in base 10
Strings
Regular Expressions
Fundamentals

More By Author:

Check out these other kata created by Uraza

Stats:

CreatedMar 9, 2015
PublishedMar 9, 2015
Warriors Trained11010
Total Skips1652
Total Code Submissions35250
Total Times Completed4480
JavaScript Completions2013
CoffeeScript Completions39
Ruby Completions484
Python Completions2055
Total Stars113
% of votes with a positive feedback rating87% of 601
Total "Very Satisfied" Votes472
Total "Somewhat Satisfied" Votes103
Total "Not Satisfied" Votes26
Ad
Contributors
  • Uraza Avatar
  • jhoffner Avatar
  • Unnamed Avatar
  • GiacomoSorbi Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • Just4FunCoder Avatar
  • farhanaditya Avatar
Ad