简体   繁体   English

如何将此 while 循环转换为 for 循环?

[英]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.首先我想告诉你,我目前正在学习javascript,遇到了一个问题。 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这是 while 循环,我无法将其传递给 for 循环

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我真的希望我能理解如何将这个 while 循环转换为 for 循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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