5. while Loops


#1


https://www.codecademy.com/en/courses/learn-javascript/lessons/loops/exercises/while


Did you write a console.log outside the for loop on the last line of the program?


var cards = ['Diamond', 'Spade', 'Heart', 'Club'];

var currentCard = 'Heart';

while (currentCard !== 'Spade') {
  console.log(currentCard);

  var randomNumber = Math.floor(Math.random() * 4);

  currentCard = cards[randomNumber];
}

console.log(currentCard);


New Javascript?
Bug: Introduction to JavaScript/ Loops/ 5. while Loops
Bug: Introduction to JavaScript/ Loops/ 5. while Loops
#2

It might be looking for a string literal - have a look at the wording of that checkpoint


#3

1.
Let's write a program that flips cards until we get a 'Spade.' Start by creating a variable named cards, and set it eqaul to this array:

['Diamond', 'Spade', 'Heart', 'Club']

2.
Right under the array, create a variable named currentCard and set it equal to 'Heart'.
This variable will hold the name of the card we just flipped. We are using 'Heart' as the first card.

3.
Let's utilize a while loop to do two things:

If the currentCard is not a 'Spade', then add a call to console.log to print the value of currentCard.
Then, create a random number between 0 and 3, and put it in a variable named randomNumber.
Then use the randomNumber to reassign currentCard to a new card from the cards array.
The while loop could like this:

while (currentCard !== 'Spade') {
  console.log(currentCard);

  var randomNumber = Math.floor(Math.random() * 4);

  currentCard = cards[randomNumber];
}

4.
Outside the while loop, on the last line of the program, use console.log to log that the program found a spade.

Run the code a few times to see the output changing. You can see the while loop guessing a card, then seeing if it is a Spade, over and over, until it finds one.


#4

Pasting instructions tells me nothing.


#5

Continuing the discussion from 5. while Loops:


https://www.codecademy.com/en/courses/learn-javascript/lessons/loops/exercises/while


Error message: Did you write a console.log outside the for loop on the last line of the program?

Written code will not be accepted, but it meets all the conditions and instructions for this task!
It seems that something is wrong with this task... I know that this course has not yet been released, but as I began to study it, I want to finish it!

Thanks for your help in advance!


var cards = ['Diamond', 'Spade', 'Heart', 'Club'];

var currentCard = 'Heart';

while (currentCard !== 'Spade') {
  console.log(currentCard);

  var randomNumber = Math.floor(Math.random() * 4);

  currentCard = cards[randomNumber];
}

console.log(currentCard);


#6

Please don't post the same thread multiple times :confused:


#7

@markkarnaukh
And if you tried

console.log("the program found a spade");

and do a refresh Browser

Always refresh the browser after making corrections:
CTRL f5 ( if on Windows or Linux)
CMD r ( if on a MAC).


#8

@leonhard.wettengmx.n now it works fine, that was very helpful! Thank you for your help and your advice, you're the best here! :slight_smile:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.