0

I'm learning JS in codecademy and it is going very well so far.

But on a particular exercise, there's a step which I can't understand. I solved it and ticked all the 3 exercise steps but the outcome is not what I expected so I was wondering if anybody can help me out?

I'm putting every step of the exercise and then what I've done.

In the end, I'll put what the console logged.

1. Below the cards array, declare a variable, currentCard, with the let keyword but don't assign it a value.

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

// Write your code below
let currentCard;

2. Create a while loop with a condition that checks if the currentCard does not have that value 'spade'. Inside the block of your while loop, add the following line of code:

currentCard = cards[Math.floor(Math.random() * 4)];

Math.floor(Math.random() * 4) will give us a random number from 0 to 3. We'll use this number to index the cards array, and assign the value of currentCard to a random element from that array.

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

// Write your code below
let currentCard;

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

3. Awesome! Your loop is running, but you can't tell because it doesn't output anything. Let's add a console.log() statement to our while block. Inside the block, after you assign currentCard a new value, log currentCard to the console.

For fun you can run your code a few times and see how the output changes!

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

// Write your code below
let currentCard;

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

So this is what the console logged:

heart
club
heart
heart
heart
spade

Every time I log the console the 'spade' appears and the idea is the console not to log 'spade'.

I hope someone can help me out here and happy coding for everyone.

3
  • Unless you use break; somewhere in your loop, it will always finish the current iteration before checking the condition again. If you don't want to log "spade", insert if (currentCard == "spade") break; right after randomly picking one. Commented Dec 6, 2018 at 19:10
  • You select the card and immediately log it before the the check can happen. Commented Dec 6, 2018 at 19:10
  • Flow chart: 3.bp.blogspot.com/-i1W59d7B8R4/T3izXShRgHI/AAAAAAAAAFI/… Commented Dec 6, 2018 at 20:29

1 Answer 1

2

The loop condition is checked at the start of the loop, not during it.

Try this instead to not see any "spade" in console:

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

// Write your code below
let currentCard;

currentCard = cards[Math.floor(Math.random() * 4)];

while ( currentCard != 'spade') {
  console.log(currentCard);
  //Assigning a new value AFTER the console.log assures it will be
  //checked against the while condition before console.log hits again
  currentCard = cards[Math.floor(Math.random() * 4)];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Again, I feel it should be pointed out that duplicating that assignment is unnecessary (and a violation of DRY) since one can simply break out of the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.