Hello Please Im trying to reverse a string using the following:
- Receive a character string as a parameter.
- Replace the vowels with the letter “V”.
- Reverse the string.
- Return the result
And this is my code, but it does not seem to work, so I need some help please. Thanks
function reverseString(str) {
let charactere = str.split("").replace(/(a)|(i)|(e)/g, ‘V’);
charactere.reverse().join(" ");
return str;
}
CodePudding user response:
You should perform the replace
first, because it must be performed on a String. split() returns an Array, and an Array does not have a replace() method.
Finally, if you return str
, you are only returning the original input of the function. Here I save the result of reversing charactere
to a new variable (revStr
) and return it instead.
function reverseString(str) {
let charactere = str.replace(/(a)|(i)|(e)/g, 'V').split("");
const revStr = charactere.reverse().join("");
return revStr;
}
console.log(reverseString('abcdefg')) // 'gfVdcbV'
CodePudding user response:
Just replace -> split -> reverse -> join
var x = "1addiiidfa"
var res = x.replace(/[aie] /g,"V").split('').reverse().join('')
console.log(res)