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

506
Visualizações
How do I create a function that takes a string and returns dashes on the left and right side of every vowel (a e i o u)

Examples dashed("Carpe Diem") ➞ "C-a-rp-e- D-i--e-m" dashed("Fight for your right to party!") ➞ "F-i-ght f-o-r y-o--u-r r-i-ght t-o- p-a-rty!" Notes

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

0

There are already a couple of solutions using regex; therefore, I am adding an answer which can be useful to someone who is not familiar with regex.

The following function, referenced by formatVowels, iterates the parameter string and uses String#indexOf to check if any of the characters is a vowel; if yes, it prepends and appends - to it and then adds the resulting text to the variable, result, otherwise, adds the character as it is to the variable, result.

const formatVowels = (str) => {
  if (str == undefined) return "";
  let result = "";
  for (const ch of str) {
    result += "AEIOU".indexOf(ch.toUpperCase()) != -1 ? "-" + ch + "-" : ch;
  }
  return result;
};

// Test
console.log(formatVowels('Hello'));

about 4 years ago · Juan Pablo Isaza Relatório

0

coolString = `Cookke`;

function dashed(coolString) {
  const regex = /a|e|i|o|u/gi;
  console.log(coolString.replace(regex, `-$&-`));
}

dashed(coolString);

about 4 years ago · Juan Pablo Isaza Relatório

0

const my_replacer = (str)=> str.replace(/[aeiouAEIOU]/g,'-$&-')
let a = 'Carpe Diem'
console.log(my_replacer(a))
let b = 'Fight for your right to party!'
console.log(my_replacer(b))

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