8. When to 'while' and when to 'for' - Code keeps crashing?


Don't know why, but my code below continuously crashes my browser (thus, there are technically no error messages). I'm not entirely sure how deep the issue is, but I feel like it should work. Essentially what I'm trying to accomplish is have the computer keep logging ("drawing") an item (cards) from the array (deck) until it reaches the king, and have it log each attempt as it does so. What's wrong?

Link - https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/1?curriculum_id=506324b3a7dffd00020bf661

var deck=["ace",2,6,8,1,"queen","jester","jester",8,10,9,2,2,"king",4,7,9,2,"joker"]

var card=false;
var cardd="none";

while(card===false) {
    for(i=0; i<deck.length; i++) {
        if(cardd==="king") {
            console.log("You got a king!");
        else {



Writing a for loop inside a while loop can be tricky. For this exercise we are free to write what we wish, but it is much simpler if we work on a for loop apart from a while loop. Best not to try doing too much in the earlier goings.

I don't wish to comment on your existing code, only to suggest as above, two separate and simple loops.


Very well. Went ahead just did a couple three-liners, less of a headache. Thanks though.


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