-1

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}`);
}
}
  • Does this answer your question? [JS Erase first instance of a vowel](https://stackoverflow.com/questions/74978232/js-erase-first-instance-of-a-vowel) – pilchard May 29 '23 at 09:15
  • your description is misleading because you don't state clear that the order of vowels is important and the deletion should happen only once in the sentence. – Alexander Nenashev May 29 '23 at 10:04

3 Answers3

0

For each vowel, replace the first occurrence in the string using String.replace(). Not to use the i flag to support lower and uppercase letters.

function disappearString(myString, toErase = 'aeiou') {
  const regex = new RegExp(`[${letter}]`, 'i');
  let result = myString;

  toErase.split('').forEach((letter) => {
    result = result.replace(regex, '');
  });

  return result;
}

const result = testStrings.map((testString) => disappearString(testString));
marcobiedermann
  • 4,317
  • 3
  • 24
  • 37
0

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?

disappearString = (myString, toErase) => {

let newString = myString.split("");
let newErase = toErase.split("");

for(let v of newErase){
    newString.indexOf(v)>=0?newString.splice(newString.indexOf(v),1):newString;
}
return newString.join("");
}

console.log(disappearString("i like javascript", "aeiou"))

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}`);
}
}
tirth1620
  • 154
  • 1
  • 8
-1

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

disappearString = (myString) => {

  myString = myString.replace(/a/i, '')
  myString = myString.replace(/e/i, '')
  myString = myString.replace(/i/i, '')
  myString = myString.replace(/o/i, '')
  myString = myString.replace(/u/i, '')

  return myString
}
LordRobin
  • 76
  • 5