Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

520
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!