There is no split
function in Number.prototype
. So, n = n + ""
is just a simple way to convert a number to a string.
From the spec
If Type(lprim) is String or Type(rprim) is String, then Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
If one of the operands in an expression with +
is a string, the other operand is also coerced to a string and concatenated with it
console.log( 1 + 1 ) // sum
console.log( 1 + "1" ) // concatenation
console.log( true + "string" ) // concatenation
In javascript , there is no Explicit declaration of datatype, by assigning value to the variable , it implicitly takes the datatype like int,string.
In your case,Simple you are applying String function to integer , so you are getting Error.
So first convert integer value into String by using "toString()" function.
Solution:
function reverse_a_number(n) {
//Casting
n=n.toString();
return Number(n.split("").reverse().join(""));
}
console.log(reverse_a_number(32243));
Cast the number to a string and then use split as numerical values don't have the split function. Again cast it to a number while returning
function reverse_a_number(n) {
n=n.toString();
return Number(n.split("").reverse().join(""));
}
console.log(reverse_a_number(32243));
The reason is that split
method works only on string values and your passing integer value as argument, that's why it's working only after casting it to string
You can't split a number. By using n = n + ""
, you're casting it a string and then splitting it. However, you're also returning a string! You'll want to cast it back to an integer before you return it.
I have an AngularJS Application I am trying to post a message through. I am successfully able to log the user in, get the access token, and I have ensured I have my domain in the JavaScript Origins ...
I have an AngularJS Application I am trying to post a message through. I am successfully able to log the user in, get the access token, and I have ensured I have my domain in the JavaScript Origins ...
I am working with parse.com and looking at making my app secure. I think I have understood well the basics principles of the ACL, CLP, and Cloud functions. My main question comes from the Part IV of ...
I am working with parse.com and looking at making my app secure. I think I have understood well the basics principles of the ACL, CLP, and Cloud functions. My main question comes from the Part IV of ...
I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the ...
I have a method for validating a string, I want that method to return a Promise as the validations being ran may be asynchronous. The issue I am having however is one of performance, I want the ...
I have an array of data objects about people. Each person object includes 0-n URLs for additional info (guests of the person). I want to process this list, calling each of the 'guest' URLs and ...
I have an array of data objects about people. Each person object includes 0-n URLs for additional info (guests of the person). I want to process this list, calling each of the 'guest' URLs and ...