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

131
Visualizações
I have to make a function which takes an array of strings and returns an array with only strings that doesn't start with a vowel

Write a function removeVowelKeys, that takes an array of strings keys and returns an array of keys that does not start with a vowel (aeiouy). The letter case does not matter.

Example :

removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']); // ['chip']

Here is my attmpt this is the max i could get out of it :

    function removeVowelKeys(keys) {
  let result = [];
  for (let i=0;i<=keys.length;i++){
    if (keys[i][0]!= 'a'||keys[i][0]!= 'e'||keys[i][0]!= 'i'||keys[i][0]!= 'o'||keys[i][0]!= 'u'||keys[i][0]!= 'y'){
      result.push(keys[i])
    }
  }
  return result;
}
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

The problem with this condition is :

if (keys[i][0]!= 'a'||keys[i][0]!= 'e'||keys[i][0]!= 'i'||keys[i][0]!= 'o'||keys[i][0]!= 'u'||keys[i][0]!= 'y')

that it will match for any alphabet. You are doing an OR check. Take for example e and do a dry run. It will psas the first condition. a will pass the second condition. And if any one of the expression is true, the whole expression returns true.

function removeVowelKeys(keys) {
  let result = [];
  for (let i=0;i<keys.length;i++){
    if (keys[i][0].toLowerCase()!= 'a'&&keys[i][0].toLowerCase()!= 'e'&&keys[i][0].toLowerCase()!= 'i'&&keys[i][0].toLowerCase()!= 'o'&&keys[i][0].toLowerCase()!= 'u'&&keys[i][0].toLowerCase()!= 'y'){
      result.push(keys[i])
    }
  }
  return result;
}

console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick'])); 

Besides that, you want your code to run till i<keys.length. Arrays are 0 based right. And to handle case, you would need to lower case as done above.

about 4 years ago · Santiago Trujillo Relatório

0

You can filter the array, and on each item check if the item startsWith a vowel character from the array with some method.

const vowelChar = ['a', 'o', 'e', 'i', 'y', 'u']
console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']));
// ['chip']
function removeVowelKeys(keys) {
  return keys.filter(item => !vowelChar.some(c => item.toLowerCase().startsWith(c)))
}

about 4 years ago · Santiago Trujillo Relatório

0

Here is another solution just incase anyone wants to use regex. It still loops through the keys and grabs the first character of each passed argument, but it checks it against the vowels.

function removeVowelKeys(keys) {
  let result = [];
  
  for (let i = 0; i <= keys.length-1; i++) {
     if(!keys[i][0].match(/[aeiuoy]/i)){
       result.push(keys[i])
     }
  }
  return result;
}

console.log(removeVowelKeys(['alarm', 'chip', 'isValid', 'Advice', 'onClick']));

about 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