Skip to content

Instantly share code, notes, and snippets.

@jastisriradheshyam
Last active July 3, 2018 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jastisriradheshyam/9a4a2d7fa955b8798eace195adea77d3 to your computer and use it in GitHub Desktop.
Save jastisriradheshyam/9a4a2d7fa955b8798eace195adea77d3 to your computer and use it in GitHub Desktop.
Java Script Tricks
function ucFirstAllWords( str )
{
var pieces = str.split(" ");
for ( var i = 0; i < pieces.length; i++ )
{
var j = pieces[i][0].toUpperCase();
pieces[i] = j + pieces[i].substr(1).toLowerCase();
}
return pieces.join(" ");
}
// Converts the string to number
// If passed string is not a number then it will return NaN (Not a Number)
function toNumber( strNumber)
{
return +strNumber;
}
@jastisriradheshyam
Copy link
Author

+strNumber is causing the string to be a positive no., in case the string is not a number then the casting fails and returns NaN (Not a Number).
It also works by using -strNumber but will result in negative no. if positive no. is passed and positive if a negative no. is passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment