3

I want to reverse a number t following the tutorial.First I have passed a number as a parameter. I have converted the number to string with to String function. Then I used split("").reverse().join("") to reverse the number. My code is as follows:

<script type="text/javascript">


        function reverse_the_string(num) {
            num = String(num);
            // str = num.toString;
            num.split("").reverse().join("");
            return;
        }
        console.log(reverse_the_string(56789));
    </script>

However I get an error undefined? Can anyone explain what is the wrong?

4
  • 2
    One liner: return String(num).split("").reverse().join(""); Commented Apr 18, 2016 at 6:54
  • @SatejS: - Step 1: num.split("") return an array with the digits from given number. For example 12345 --> [ '1', '2', '3', '4', '5' ] - Step 2: .reverse() return an array with elements in reverse order. So [ '1', '2', '3', '4', '5' ] will become [ '5' , '4', '3', '2', '1' ] - Step 3: .join('') is Array method to create a string from elements. So [ '5' , '4', '3', '2', '1' ] will becomes '54321' The approach is correct. He just forget return value :) Commented Apr 18, 2016 at 7:00
  • What i did was return instead of return num, I think that return too passes the result? I want to know why does return only do not work? Commented Apr 18, 2016 at 7:04
  • You need to specify what you want to return. There is no way the JavaScript parser can guess which variable you want to be returned. Therefore you need to specify it yourself. Imagine being an a shop and you ask the employee to sell you something instead of to sell you a blue ball. For the first question the employee still don't know what you want. Commented Jul 3, 2019 at 12:12

5 Answers 5

5

You do not return the result. Use

return num.split("").reverse().join("");

If you use just return, then undefined is returned.

function reverse_the_string(num) {
    return String(num).split("").reverse().join("");
}

document.write(reverse_the_string(56789));

Caveat: Reversing works only with small numbers!

Sign up to request clarification or add additional context in comments.

6 Comments

Something has to be done if input is 10101010101010101010101010
thats a problem. but i think, that does not matter actually.
What i did was return instead of return num, I think that return too passes the result? I want to know why does return only do not work?
@RayonDabre—not a problem if it's "10101010101010101010101010". ;-)
@pallavidestiny22, that is, how the syntax works in javascript, please see the documentation for the return statement.
|
2

The solution of casting the number into a string and reversing the string is a good solution, BUT it doesn't cover all the cases. There are 2 cases that need to be addressed:

  1. Reversing a negative number it doesn't retain the sign.
  2. Reversing a number that ends with zero(s), after reversing you will have leading zeros, e.g. 12300 the reverse will be "00321" when it should be "321".

Using Regular function

var reversed = parseFloat(num.toString().split("").reverse().join()) * Math.sign(num);

Using Arrow Function

var reversed = num => parseFloat(num.toString().split('').reverse().join('')) * Math.sign(num);

Comments

2

Try this (Recursive Method)

 
function reverseNum(num) {
    if(num == 0)return 0;
    let x = Math.pow(10, (num + '').length - 1);
    return (num % 10) * x + reverseNum(Math.floor(num / 10));
} 
console.log(reverseNum(432));

Comments

0

Convert the number variable to a string:

var n = 4557
n = n.toString();

then execute the below code.

 //**Reverse the Number In javascript** 
 var result = "";
 function reverseNumber(n){
   // reverse the number via loop 
   for(var i=x.length-1; i>=0; i--){  
     result+=n[i];
   }
   return Number(result)
 }
 var x = prompt("Enter a number : ");
 console.log(reverseNumber(x))
            

Comments

-2

In order to reverse number/ string you can refer this method: (without using slice() and substring() methods)

function reverse(str) {
  var rev = "";
  number = parseInt(str);
  for (; number > 0;) {
    rev += number % 10;
    number = Math.floor(number / 10)
  }
  return rev;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.