I am creating a program in javascript which deletes the first instance of a vowel in a word. eg
original text: quick brown fox jumps over the lazy dog
output: th qck brwn fox jumps over the lzy dog
I cannot get the function to work, it always outputs the original sentence with no changes
disappearString = (myString, toErase) => {
let newString = myString.split();
let newErase = toErase.split();
for(let i = 0; i < newString.length; i++){
if(newString[i] === newErase[i]){
console.log(newString.delete(toErase[i]));
}
}
return newString.join();
}
let testStrings = [
"the quick brown fox jumps over the lazy dog",
"hello world",
"software engineering is fun",
"i like javascript",
"clown case",
"rhythms"
]
let stringToDisappear = "aeiou"
let correctStrings = [
"th qck brwn fox jumps over the lzy dog",
"hll world",
"sftwr engneering is fn",
" lik jvascript",
"clwn cs",
"rhythms"
]
for (let strIdx = 0; strIdx < testStrings.length; strIdx++) {
let test = testStrings[strIdx];
let correct = correctStrings[strIdx];
let got = disappearString(test, stringToDisappear);
if (got == correct) {
console.log(`${strIdx + 1}: Testing ${test}: Correct!`);
} else {
console.log(`${strIdx + 1}: Testing ${test}: Wrong, got ${got}, expected ${correct}`);
}
}
3
Answers
For each vowel, replace the first occurrence in the string using
String.replace()
. Not to use thei
flag to support lower and uppercase letters.So what I understood you are trying to achieve is to remove the first occurence of each vowel in a string (being a word or a whole sentence).
This is a quick implementation
Hey, Let’s solve your program step wise:
First make your function work correctly with every cases.
Please also learn difference about == and ===
Which equals operator (== vs ===) should be used in JavaScript comparisons?