Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

522
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda