Write A JavaScript Function That Reverse A Number & Explanation
Write A JavaScript Function That Reverse A Number & Explanation
function reverse_a_number(n)n=32243
{
n = n + "";
return n.split("").reverse().join("");
}
console.log(Number(reverse_a_number(32243)));
Sample Output:
34223
Explanation:
Assume n = 1000.
Convert a number to a string :
Code : -> n = n + "";
Note : There are different ways to convert number to string :
The split() method is used to split a String object into an array of strings by separating the
string into substrings.
Code : console.log('1000'.split(""));
Output : ["1", "0", "0", "0"]
The Number constructor contains constants and methods for working with numbers.
Values of other types can be converted to numbers using the Number() function.
Output : 1