0

I have a simple question to ask, in which I am slightly embarrassed to ask, but I realize that I won't learn unless I ask.

When looking at reversing a string, I recognize that reversing a string requires you to split up the string, reverse it, and then re-join it. Like so.

var stringReverse = function (n){
    return n.split("").reverse().join("");
}
console.log(stringReverse("hello"));

However, I was trying to reverse a number, much of the principles were very similar except for one part. The code below is:

var reverse_a_number = function (n){  
    n = n + "";  
    return n.split("").reverse().join("");  
}  
console.log(reverse_a_number(32243));  

My question is why am I needed to bring in the " n = n + " ";" to the code? I was assuming that the same principles would be similar regardless of it being a string, or an integer.

Much thanks in advance, and I apologize that this question is elementary.

kdweber89
  • 1,984
  • 2
  • 19
  • 29
  • i think when you write in integer n = n+ 1 , that means you add 1 in n, but in string when you write n= n+ a , that means you concatenating a in n and in integer you don't get split method i hope you got your answer. – Sumit Jha Apr 08 '15 at 16:12

6 Answers6

1

why am I needed to bring in the " n = n + " ";" to the code?

Adding + "" will make a cast to String, this way you can use String functions (.split).

The integer have only functionality of a Number. If you want to treat the Number as a String you need to cast it (+ "") or convert it (.toString())

The .reverse() function come from Array object that is created when you use String function .split().

With .join() you come back to String from Array (of characters).

If you want to come back to Number after reverting, you can choose one of these functions.

Community
  • 1
  • 1
adricadar
  • 9,971
  • 5
  • 33
  • 46
0

To put it simply, the docs require it to be a string. You could combine your two methods by doing exactly what you're doing in reverse_a_number (it works for both). Also, don't be embarrassed to ask questions, it's how you learn :)

william.taylor.09
  • 2,145
  • 10
  • 17
0

A number doesn't have the split method. You have to convert it into a string, which does have split.

You can use typeof to find out the type of a variable. The following code snippet demonstrates the types of a number and a string, and shows the value of the split attribute for each one. Note that split is undefined for the number.

var n = 42;
document.write(typeof n, '<br />', n.split, '<br />', '<br />');

var s = ''+n;
document.write(typeof s, '<br />', s.split);

If you want to reverse an integer by treating it as an integer, without using string operations, you can do the following.

function reverseInteger(n) {
  var result = 0;
  while (n != 0) {
    var digit = n%10;
    result = 10*result + digit;
    n = (n-digit)/10;
  }
  return result;
}

var n = 3141590123456;
document.write(n, '<br />');
document.write(reverseInteger(n));

Note that the last digit of n is n%10. If we subtract the last digit and divide n by ten, the effect is to shift n one digit to the right. We keep doing this and build up result in reverse by multiplying it by ten on each iteration and adding the digit that we just removed from n.

Michael Laszlo
  • 12,009
  • 2
  • 29
  • 47
0

Number doesn't have reverse and split method directly and you should convert number to string that be able reverse it.

to convert it you can add an empty string after it, like you.

just it.

0

Javascript sets the type of a variable at runtime.

If she (yes, it's a girl) sees that you only have ciphers, she will consider it's an integer.

Adding + ""; casts it into a string, which is an array of chars.

Your string is stored a lil bit like : ['h', 'e', 'l', 'l', 'o']
an integer is stored as {0011001010101...}

Deblaton Jean-Philippe
  • 11,188
  • 3
  • 49
  • 66
0

Explanation

You are passing in a number, not a string. When you combine a number with a string ("") it assumes you want to make a string. So you are really converting it to a string. To an array, then back to a string. If you attempt to split a number, the compiler will throw an error. More exactly

TypeError: undefined is not a function (evaluating 'n.split('')')

So what you really are doing is making the number into a String. There are different methods you can use, take a look here.

n += '';(String) ->

.split() (Array) -> .split() (Array) -> .join() (String)

Your function is actually producing and returning a string

Alternative Function

I'm going to critique ;) , you could shorten, and improve this function using the following.

var reverse_a_number = function (n){  
    return +(n += "").split("").reverse().join("");  
}  

What does this do?

n = n + '' has a special operator, +=. We can shorten this to on-line by using this inside parenthesis. The + will convert it into a Integer

Community
  • 1
  • 1
Downgoat
  • 13,771
  • 5
  • 46
  • 69