mercredi 8 septembre 2021

the less than operator in javascript (<) is working as less or equals to [duplicate]

this code is supposed to display a random character/ letter of a given name in javascript using while loops and if statements . . . the problem that I faced is that the RandomLetterIndex is between 0 and 5 (<=5) when I want it to be between 0 and 4 (<5)

const MyName = "Ayman";
var RandomLetterIndex = Math.floor(Math.random() * 10);

while (RandomLetterIndex > MyName.length) {
  RandomLetterIndex = Math.floor(Math.random() * 10);
  if (RandomLetterIndex < MyName.length && RandomLetterIndex !== 5) {
    break
  }
}

console.log(RandomLetterIndex);
console.log(MyName.charAt(RandomLetterIndex));

Aucun commentaire:

Enregistrer un commentaire