Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
JS switch statement works only sometimes

I'm writing a program that removes vowels from str parameter and I've decided to use a switch statement to achieve this goal. I'm pretty bad at explaining stuff so I'll give you an example:

No offense but,\nYour writing is among the worst I've ever read

Should become

N ffns bt,\nYr wrtng s mng th wrst 'v vr rd

But my output looks like this:

N ffns bt,\nYur wrtng s mng th wrst 'v vr rad

The program decided to leave "u" and "a" for some reason. After nearly 1,5h of looking for a cause, I give up because I have literally no clue what I did wrong.

function disemvowel(str) {
  let arr = str.split(''); // str -> arr
  for (let x = 0; x <= arr.length; x++) { // remove vowels
    switch(arr[x]) {
      case "a":
      case "e":
      case "i":
      case "o":
      case "u":
      case "A":
      case "E":
      case "I":
      case "O":
      case "U":
        arr.splice(x, 1);
        break;  
    }
  }
  str = arr.join([separator = '']) // arr -> str
  return str;
}

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The array length is changing after every splice. You can define the length at first and decrement x every time you delete an item:

function disemvowel(str) {
  let arr = str.split(''); // str -> arr
  const length = arr.length;
  for (let x = 0; x < length; x++) { // remove vowels
    switch(arr[x]) {
      case "a":
      case "e":
      case "i":
      case "o":
      case "u":
      case "A":
      case "E":
      case "I":
      case "O":
      case "U":
        arr.splice(x, 1);
        x--;
        break;  
    }
  }
  str = arr.join([separator = '']) // arr -> str
  return str;
}

console.log(
  disemvowel("No offense but,\nYour writing is among the worst I've ever read")
);

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda