Reverse a String: I have a error ".split is not a function"

Tell us what’s happening:
I was struggling with challenge.
I though I made SPLIT -> RESERVE -> JOIN as step.
However, I have a error message “text.split is not a function”. and I have no idea why this is not working…
(1) Is split function not able to read array data?
(2) When I made var text = [str], is ‘hello’ inserted to array[0] ?

Your code so far
//my Answer
function reverseString(str) {
var text = [str];
var splitWords = text.split(’ ');
var reserveWords = splitWords.reverse();
var joinWords = reserveWords.join();
return joinWords;
}

//Hint’s Answer
function reverseString(str) {
return str.split(’’).reverse().join(’’);
}

reverseString(“hello”);```
Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36.

Link to the challenge:

The split method works on strings, but you have tried to apply it to an array, which doesn’t work.

Split is used to turn a string into an array…

2 Likes

you cant declare var text = [str]
but you can use several other ways

let text = Array.from(str)

let text = []

text[0] = str
//or
text.push(str)
//or
text.unshift(str)

and if you want to split the string into an array like: hello will split to [h,e,l,l,o]

let text = str.split("")

1 Like