Why Is .join(" "); Not Working On Variable That Points To Reversed Array?
If I chain it: reverser.split(' ').reverse().split(' '), it works. But, if I split, reverse, then join the variable -- it doesn't perform the join: var s = 'How are you'; function
Solution 1:
The .join()
function returns a string. It doesn't transform the target array into a string.
So,
reverser = reverser.join(" ");
Solution 2:
joins returns a string and you have to store it in the variable.
var s = "How are you";
functionreverser(str){
var reversed = str.split(" ");
reversed.reverse();
reversed=reversed.join(" ");
return reversed;
};
console.log('doesn\'t work ', reverser(s));
Solution 3:
split() splits the string into array of strings. You need to store the resulting output everytime so that next operation can be performed right.
var s = "How are you";
functionreverser(str){
var reverser = str.split(" ");
var revArray=reverser.reverse();
var revArrayJoined=revArray.join(" ");
return revArrayJoined;
};
console.log(reverser(s));
//output: you are How
Post a Comment for "Why Is .join(" "); Not Working On Variable That Points To Reversed Array?"