skip to Main Content

I have an assignment to push these to the array then take one away. I’m having issue figuring out how to console log the third property of this array.

I tried a few different approaches but no dice. I’m new so I don’t want to post exactly what I tried as we know it doesn’t work or I wouldn’t be here.

var clothes = [];
clothes.push("Jorts");
clothes.push("White Tank top");
clothes.push("Sweat Pants");
clothes.push("Sandals and sox");
clothes.push("Affliction T shirt");
clothes.pop("Affliction T shirt");

console.log(clothes);

2

Answers


  1. It looks like you’re on the right track. To console log the third property of the array, you can use the following code:

    console.log(clothes[2]);
    

    This will print "Sweat Pants" to the console, which is the third element in the array. Remember that array indices are zero-based, so the first element is at index 0, the second element is at index 1, and so on.

    Login or Signup to reply.
  2. The problem you have is that currentArray gets its value asynchronously, which means you are calling setAllEvents too soon. At that moment the allCallBack function has not yet been executed. That happens only after the current running code has completed (until call stack becomes emtpy), and the ajax request triggers the callback.

    So you should call setAllEvents and any other code that depends on currentArray only when the Ajax call has completed.

    NB: The reason that it works in the console is that by the time you request the value from the console, the ajax call has already returned the response.

    Without having looked at the rest of your code, and any other problems that it might have, this solves the issue you have:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search