Reputation: 1328
Write a function that removes the last vowel in each word in a sentence.
Examples:
removeLastVowel("Those who dare to fail miserably can achieve greatly.") ➞ "Thos wh dar t fal miserbly cn achiev gretly." removeLastVowel("Love is a serious mental disease.") ➞ "Lov s serios mentl diseas" removeLastVowel("Get busy living or get busy dying.") ➞ "Gt bsy livng r gt bsy dyng"
What I am doing is
function sumDigProd(arr) {
let ans = arr.split(" ");
let array = [];
for (i = 0; i < ans.length; i++) {
for (j = 0; j < ans[i].length; j++) {
var vowelAtLast;
if (
ans[i][j].toLowerCase() == "a" ||
ans[i][j].toLowerCase() == "e" ||
ans[i][j].toLowerCase() == "i" ||
ans[i][j].toLowerCase() == "o" ||
ans[i][j].toLowerCase() == "u"
) {
vowelAtLast = ans[i][j];
}
}
var idex = ans[i].lastIndexOf(vowelAtLast);
console.log(idex,ans[i],ans[i][idex]);
ans[i].replace(ans[i][idex],'')
array.push(ans[i])
console.log(ans)
}
console.log(ans)
return array.join(" ");
}
console.log(sumDigProd("youuo goalo people."));
Here, console.log(idex,ans[i],ans[i][idex]);
gives me the correct output for example:
4 "youuo" "o"
But now when I try to do:
ans[i].replace(ans[i][idex],'')
console.log(ans)
I get
["youuo", "goalo", "people."]
again instead i should get
["youu", "goalo", "people."]
after the first time loop runs... and then at last I get the output as
["youu", "goal", "peopl."]
as per the problem but I get
["youuo", "goalo", "people."]
why are no changes made by the replace
method here?
Upvotes: 2
Views: 743
Reputation: 371233
A problem is that
ans[i].replace(ans[i][idex], '')
will only replace the first occurrence of whatever character ans[i][idex]
is. Eg
aza
would result in
za
Another issue is that you must use the return value of .replace
, else it'll go unused and be irrelevant; you'd want something like
ans[i] = ans[i].replace(ans[i][idex], '')
instead, so that the item at that index in the array is properly reassigned.
But it would probably be easier to use a regular expression: match a vowel, followed by capturing zero or more non-vowels in a capture group, with lookahead matching a space or the end of the string. Then replace with the first capture group, thereby removing the last vowel:
const sumDigProd = str => str.replace(
/[aeiou]([^aeiou]*?)(?= |$)/gi,
'$1'
);
console.log(sumDigProd("youuo goalo people."));
[aeiou]([^aeiou]*?)(?= |$)
means:
[aeiou]
- Any vowel([^aeiou]*?)
- Match and capture:
[^aeiou]*?
Any non-vowel, lazily (capture group), up until(?= |$)
- lookahead matches a space or the end of the stringThen
'$1'
- Replace with the capture groupTo change your original code, identify the last index of a vowel by iterating from the final index of the string and going backwards. When one is found, reassign the string at ans[i]
and .slice
ing the portions behind and in front of the found vowel:
function sumDigProd(arr) {
let ans = arr.split(" ");
for (i = 0; i < ans.length; i++) {
for (j = ans[i].length - 1; j >= 0; j--) {
if (
ans[i][j].toLowerCase() == "a" ||
ans[i][j].toLowerCase() == "e" ||
ans[i][j].toLowerCase() == "i" ||
ans[i][j].toLowerCase() == "o" ||
ans[i][j].toLowerCase() == "u"
) {
ans[i] = ans[i].slice(0, j) + ans[i].slice(j + 1);
// go to next word
break;
}
}
}
return ans.join(' ');
}
console.log(sumDigProd("youuo goalo people."));
Upvotes: 4