3

I want to say first. Very beginner. Be kind. However. I am attempting to reverse a string. Put a string into a function as an argument and reverse its order. For example 'hello' should return 'olleh', etc.

I have complete this by doing:

function revString(stringArg) {
    return stringArg.split("").reverse().join(");
}

However. By doing it like this:

function revString (stringArg) {
    stringArg.split("");
    stringArg.reverse();
    stringArg.join("");
    return stringArg;
}

The resulting output for the second function is that reverse() is not a function. Can someone tell me what the difference here is?

Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
user2613041
  • 43
  • 1
  • 8

5 Answers5

1

This happens because split() does not split the string in place, but instead returns the split string as a new value. In your second function, you do stringArg.split(), but since that operation doesn't happen in place, stringArg is never mutated. It retains the exact same value and type throughout, which leads to the error you're seeing.

The reason the first function works is because the function calls are applied to the return value of the previous call. In other words:

  • split() is called on stringArg
  • reverse() is called on the returned value of split(), which is an array and therefore can be directly reversed
  • join() is called on the returned value of reverse()

But in the second function you're calling them all on the exact same variable, stringArg, which stays the same throughout. In order for your function to work you need to modify it like so:

function revString (stringArg) {
    a = stringArg.split(""); // Array
    b = a.reverse(); // Reversed array
    c = b.join(""); // String
    return c;
}

However this, while easier to understand perhaps, is needlessly verbose. I recommend sticking with the original function.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
1

This is because .reverse() is used on the output of .split("").

Using .reverse() by itself doesn't work because the line before it doesn't directly change it unless you reassign it with stringArg = stringArg.split("");. The first line would return the array of characters but doesn't change stringArg directly.

So:

stringArg.split("").reverse().join("");

means to join all elements within the array whose elements are in reverse order based on the spliting of the string. In other words, .reverse() is used on stringArg.split(""), not just stringArg and same with .join(""): it is applied to stringArg.split("").reverse() not just stringArg.

Thus the solution would be:

function revString2 (stringArg) {
    a = stringArg.split("");
    b = a.reverse();
    c = b.join("");
    return c;
}
Anthony Pham
  • 3,096
  • 5
  • 29
  • 38
0

The first example chains the results of previous method call, where .split() returns an array as first link in chain.

The second example performs a separate task on the original string at each line, not chained to the first line which returns an array.

.reverse() is not a method defined at String.prototype, resulting in the error at stringArg.reverse() at second line of function call.

guest271314
  • 1
  • 15
  • 104
  • 177
0

String#split function doesn't work in situ (doesn't mutate the original string). That's why you can't use Array#reverse function which can be called only on arrays. You could try to assign the new, received array into a new variable, then use Array#reverse, which works in situ (mutates the original array) and then, return joined array.

function revString(stringArg) {
    let arr = stringArg.split("");
    arr.reverse();
    return arr.join("");
}

console.log(revString('hello'));
kind user
  • 40,029
  • 7
  • 67
  • 77
0

String.split() returns a new value Which is an array of all the characters in str Array.reverse returns an array with its content reversed Join returns a string These functions don't change their values though reverse may, I'm not sure. Below the stringArgs variable is set to the return value of each function Try:

function revString ( stringArg ) { 
stringArg=stringArg . split ( "" ); 
stringArg=stringArg . reverse (); 

stringArg=stringArg . join ( "" ); return stringArg ; } 
user7951676
  • 377
  • 2
  • 10