lundi 18 octobre 2021

if else print at the same time in JavaScript

when I trigger the if condition, why the else will print out as well? someone could indicate what's the problem of the code below? written in JS.

original picture of code

const rainbow = ['red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'violet'];
let colour = prompt("What is your favourite colour?").toLowerCase();

for (let i = 0; i < rainbow.length; i+=1) {
  if (rainbow[i] === colour) {
    console.log("yes")
  }
  else {
    console.log(rainbow.join(" *** "));
  }
}

Aucun commentaire:

Enregistrer un commentaire