简体   繁体   中英

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

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); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM