skip to Main Content

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


  1. 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));
    
    Login or Signup to reply.
  2. 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
    }
    
    Login or Signup to reply.
  3. 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}`);
    }
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search