Epic Karma
Epic Karma

Reputation: 1

How can I convert this while loop to a for loop?

First of all I want to tell you that I am currently learning javascript, and I have run into a problem. I have tried most of the methods I have learned so far and could not do it. If someone could tell me how it works I would appreciate it.

This is the while loop I can't pass it to a for loop

const cards = ['diamond', 'spade', 'heart', 'club'];

let currentCard;

while (currentCard != 'spade') {
   currentCard = cards[Math.floor (Math.random () * 4)];
   console.log(currentCard);
}

I really wish I could understand how to take this while loop to a for loop

Upvotes: 0

Views: 54

Answers (1)

Boris Lipschitz
Boris Lipschitz

Reputation: 1631

Well, this is kinda cheating, buy hey, this isn't wrong :-)

const cards = ['diamond', 'spade', 'heart', 'club'];

let currentCard;

for (;currentCard!='spade';) {
  currentCard = cards [Math.floor (Math.random () * 4)];
  console.log (currentCard);
}

Upvotes: 3

Related Questions