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

  • SEARCH
  • Home
  • 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 6226009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:56:55+00:00 2026-05-24T08:56:55+00:00

Im trying to learn javascript by tracing through some code at the moment, I

  • 0

Im trying to learn javascript by tracing through some code at the moment, I was wondering if someone could explain what is happening in this snippet of code (this snippet is just part of a function hence no closing brace):

document.addEventListener("keydown",function(e){

        for(i=0;i < keys.length; i++) {

            if(e.keyCode == keys[i]){

            var tri = document.getElementById("foo").childNodes[i];

            if(i==0){
                var tri = document.getElementById("foo").childNodes[1];
            }

            if(i==1){
                var tri = document.getElementById("foo").childNodes[3];
            }

            if(i > 1) {
                var tri = document.getElementById("foo").childNodes[(i*2)+1];
            }

The part im confused about most in this is the childNodes[] and the if(i) statements?

  • 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-05-24T08:56:57+00:00Added an answer on May 24, 2026 at 8:56 am
    // Bind an event handler to keydown on the entire document
    document.addEventListener("keydown",function(e){
    
        // Everything in here happens on keydown
    
        // keys must be an array declared somewhere earlier in the code
        // This loops through that array
        for(i=0;i < keys.length; i++) {
    
            // If the current key we are looking at in the array
            // is the key that was pressed
            if(e.keyCode == keys[i]){
    
                // Get the (i+1)th childnode of foo
                var tri = document.getElementById("foo").childNodes[i];
    
                // If i = 0 get the second element (not the first)
                if(i==0){
                    var tri = document.getElementById("foo").childNodes[1];
                }
    
                // If i == 1 get the fourth element (not the second)
                if(i==1){
                   var tri = document.getElementById("foo").childNodes[3];
                }
    
                // Otherwise get the (i*2+2)th element.
                if(i > 1) {
                    var tri = document.getElementById("foo").childNodes[(i*2)+1];
                }
    
                // Here we are still in an if-statement, in a loop, in a function, 
                // so there is probably more code here, at least some closing }'s
    

    Note that var tri = document.getElementById("foo").childNodes[i]; is a useless line, because i cannot be negative, one of the next three if statements will always succeed and tri will be overwritten.

    Also note that when i = 0, (i*2)+1 = 1 and when i = 1, (i*2)+1 = 3, so those other two if statements are useless as well because the third covers all cases and doesn’t need to even be in an if clause. The above code is 100% equivalent to:

    document.addEventListener("keydown",function(e){
    
        for(i=0;i < keys.length; i++) {
    
            if(e.keyCode == keys[i]){
    
                var tri = document.getElementById("foo").childNodes[(i*2)+1];
    
                ...
    

    Since i is the variable used to iterate through the array called keys, and the node selected depends on i. keys must be an array with an unusual purpose. It is an array of keyCodes, where the position of the keyCode in the array determines which node should be selected and stored in tri when that key is pressed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn about this feature of javascript I keep seeing in code,
I'm trying to learn jquery, I've some basics of javascript. I've written this piece
I'm currently trying to learn OO Javascript so I can write some cleaner code.
I have some javascript that I'm trying to retool using jQuery to learn the
Trying to learn a bit about PDO and is going through this tutorial .
This problem should be simple enough, i am trying to learn animation using javascript
I'm trying to learn some javascript and have gotten stuck. Right now, I have
I am trying to learn JavaScript. After reading this page: What does ':' (colon)
I'm trying to learn myself some C++ from scratch at the moment. I'm well-versed
Hi I'm trying to learn javascript and I'm messing around with this example I

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.