0

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.

kanzyxx
  • 1
  • 1
  • When I execute your code, I got a syntax error. – The KNVB Jun 18 '22 at 04:42
  • Remove else statement and you code works. Else statement is not compulsory. – krupali makadiya Jun 18 '22 at 04:45
  • 1
    The places that you've mentioned, will wrap your code in a function which means that when a `return` statement is reached, the code will stop executing. The code below will not be executed. – Titus Jun 18 '22 at 04:54
  • Your code is using `return`, is what you've shared inside of a `function`? Can you please provide code that is runnable so that we can reproduce your issue? See [mre] – Nick Parsons Jun 18 '22 at 04:57
  • @NickParsons I've edited the code again according to the link you've mentioned and hopefully now it's easier to understand @Titus thank you for your explanation I was very confused in the moment I went through this because I didn't know the `return` would just stop the loop altogether. It makes more sense now when only the first vowel would be saved to the `resultArray` – kanzyxx Jun 18 '22 at 16:08
  • 1
    @kanzyxx Thanks, your current code results in the error of `"Uncaught SyntaxError: Illegal return statement"`, however you don't seem to mention that error in your question and instead say it doesn't show an output, so it makes me think that the code you are running is still different. `return` only works within a function. If your real code is actually within a function (which you should show in the question if it is), then the issue is as Titus described [Does return stop a loop?](https://stackoverflow.com/q/11714503) – Nick Parsons Jun 19 '22 at 01:39

1 Answers1

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);
JoelCrypto
  • 462
  • 2
  • 12
  • 1
    That is not the problem, the OP is asking why the `console.log(resultString);` is not executed when those `else` statements are in the code. – Titus Jun 18 '22 at 04:49
  • Because there is a syntax error, i said it at the beggining. – JoelCrypto Jun 18 '22 at 04:50
  • Oh, that is not a syntax error if the code is inside a function (need that because of `return`), omitting the brackets of conditional statements is valid syntax. – Titus Jun 18 '22 at 04:52
  • Ok. Nonetheless this was not in a function there so it goes into error and even if it was, it would only return false. Both ways, thé script was unsuccessful. But i have changed my answer. – JoelCrypto Jun 18 '22 at 04:54