Working on this reverse words in string
I tried to revert part of the string, given the input string, start_index, end_index, but it seems no effect.
var myrev = function(str, s, e) {
let tmp;
while(s<e) {
tmp = str[s];
str[s] = str[e];
str[e] = tmp;
s++;
e--;
}
return str;
}
full code
// revert the words in this string: " the sky is blue "
var reverseWords = function(s) {
let a = s.trim().replace(/\s+/g, ' ');
a = a.split('');
// s === "eulb si yks eht"
s = a.reverse().join('');
let i, j, ind;
ind = s.indexOf(' ', 0);
i=0;
j=ind-1;
// expect s === "blue si yks eht", but still "eulb si yks eht", why???????
s = myrev(s, i, j);
console.log(s)
};
var myrev = function(str, s, e) {
let tmp;
while(s<e) {
tmp = str[s];
str[s] = str[e];
str[e] = tmp;
s++;
e--;
}
return str;
}
Does it seem I need to pass a string as a reference?