Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8359173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:03:49+00:00 2026-06-09T11:03:49+00:00

I’m trying to learn JavaScript in Codecademy and one of the things mentioned briefly

  • 0

I’m trying to learn JavaScript in Codecademy and one of the things mentioned briefly are “for in” loops. I’ve used them in a few of the exercises, but in this case I can’t get it to work. I could do it with for (i = blah; etc), but I’d like to know what’s wrong with this and if there’s a way to fix it:

//Your three dimensional array from the last exercise probably
//looked something like this:
var hands = [];
hands[0] = [ [3,"H"], ["A","S"], [1,"D"], ["J","H"], ["Q","D"] ];
hands[1] = [ [9,"C"], [6,"C"], ["K","H"], [3,"C"], ["K","H"] ];

//Loop over every dimension in the array, logging out the suit and rank
//of each card in both hands
//1. loop over each hand
for (var hand in hands) {
//2. loop over each card array in each hand
    for (var card in hand) {
        //3. loop over each rank/suit array for each card in each hand
        for (var prop in card) {
            //4. log the value of the rank/suit array item
            console.log(card[prop]);
        }
    }
}

The output is 0 0 0, instead of the number and suit. I tried placing console.log() after the first and second loop and I noticed it works correctly in the first, but not in the second one.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:03:51+00:00Added an answer on June 9, 2026 at 11:03 am

    Momentarily ignoring the for-in issue, your inner loops should look like this…

    for (var hand in hands) {
        for (var card in hands[hand]) {
            for (var prop in hands[hand][card]) {
                console.log(hands[hand][card][prop]);
            }
        }
    }
    

    Notice that each inner loop needs to explicitly reference the current value of its outer loop. You are iterating over the keys (the variable before in) instead.


    To deal with Arrays properly, you should almost always use a for loop instead of for-in.

    for (var i = 0; i < hands.length; i++) {
        for (var j = 0; j < hands[i].length; j++) {
            for (var k = 0; k < hands[i][j].length; k++) {
                console.log(hands[i][j][k]);
            }
        }
    }
    

    There are a few reasons for this, which you can find on StackOverflow.


    To add clarity to the code, you can cache the current item in a variable…

    for (var i = 0; i < hands.length; i++) {
        var hand = hands[i]
        for (var j = 0; j < hand.length; j++) {
            var cards = hand[j];
            for (var k = 0; k < cards.length; k++) {
                console.log(cards[k]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.