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

202
Visualizações
How to print the vowels of the input str without duplicates in JavaScript

This is my code thus far, it duplicates vowels e.g.:

  • Input: baloon
  • Current output: a,o,o
  • Expected output: a,o

How can I fix this?

function printOutVowels(str) {
  let vowels = "";
  for (let i = 0; i < str.length; i++) {
    if (str[i] == "a" || str[i] == "e" || str[i] == "i" || str[i] == "o" || str[i] == "u") {
      vowels = str[i]
      console.log(vowels);
    }
  }
}

printOutVowels("timidity");

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

One solution is using Set data structure. A set is like an array, but contains no duplicated element.

function printOutVowels(str) {
  // Create an empty set
  const vowels = new Set();
  
  for (let i = 0; i < str.length; i++) {
    if (str[i] == "a" || str[i] == "e" || str[i] == "i" || str[i] == "o" || str[i] == "u") {
      // Add element to the set
      vowels.add(str[i]);
    }
  }

  // Print the set content
  for (let item of vowels) {
    console.log(item);
  }
}

printOutVowels("timidity");

Behind the scene, Set use its has method to check if an item is already in the set. This method is faster than Array.prototype.includes when a set and an array have the same size.

about 4 years ago · Juan Pablo Isaza Relatório

0

You could do that :

function printOutVowels(str){
    let vowels = "aeiou";
    let output = "";
    for (let i = 0; i < vowels.length; i++) {
      if(str.toLowerCase().includes(vowels[i])) output += vowels[i];
    }
    return output;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Splitting and filtering is a concise way to get the vowels. Running them through a set will enforce uniqueness. (Force to lowercase to handle both cases)

function vowels(str) {
  const isVowel = l => /^[aeiou]$/.test(l);
  const vowels = new Set(str.toLowerCase().split('').filter(isVowel));
  return Array.from(vowels.values());
}

console.log(vowels('TIMidity'))

about 4 years ago · Juan Pablo Isaza 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