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

513
Visualizações
if (condition) else return statements where there is no output even when the if block is true

Hello I've just started learning JS and currently doing tutorial project on loops, basically taking the phrase "coconut closet" and translating it into "whale talk" equivalent = oouoe

I've already learnt that the else block is optional where you can have an if without an else; but on my first try when not knowing this, I included an else statement to the if statement. This in turn, when run on the console it had no ouput. Here is the code in question below:

let input = "coconut closet";

const vowels = ["a", "e", "i", "o", "u"];

let resultArray = [];

for (let i = 0; i < input.length; i++) {
  for (let j = 0; j < vowels.length; j++) {
    if (input[i] === vowels[j]) {
      resultArray.push(vowels[j]);
    } else return; 
  }
}

let resultString = resultArray.join('')
console.log(resultString);

I've tried to look for an explanation but currently unable to find any, can someone please explain why my code has no output?

I've tried this both on codecademy's IDE and VS Node.

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

0

You should check the console when executing your code, your else statement returns false outside of a function and inside it, it will stop the end of the function. And it is useless.

let input = "coconut closet";

const vowels = ["a", "e", "i", "o", "u"];

let resultArray = [];

for (let i = 0; i < input.length; i++) {
  if (input[i] === "e") {
    resultArray.push("e");
  } else if (input[i] === "u") {
    resultArray.push("u");
  }

  for (let j = 0; j < vowels.length; j++) {
    //console.log(`j is ${j}`);
    if (input[i] === vowels[j]) {
      //console.log(input[i]);
      resultArray.push(vowels[j]);
    }
  }
}

//console.log(resultArray);
let resultString = resultArray.join('').toUpperCase();
alert(resultString);
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