Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

.push() method is not working correctly?

So I have this code over here:

const TTest2 = new Array();
const APindex = new Array();
const HONORSindex = new Array();
const CPindex = new Array();

const test = [0];


for(let i = 0; i < convertToLetter.length; i += 1) {
  console.log(i, convertToLetter[i][0]);
  if(convertToLetter[i][0].toUpperCase() === "AP") {
    console.log(i);
    TTest2.push(i);
    APindex.push(i);
    test.push(i);
  } else if(convertToLetter[i][0].toUpperCase() === "HONORS") {
    HONORSindex.push(i);
  } else if(convertToLetter[i][0].toUpperCase() === "CP") {
    CPindex.push(i);
  }
}

I have these 3 arrays, APindex, HONORSindex, and CPindex, each of which are taking values from the Array convertToLetter. However, APindex and TTest2 do not contain the same values, however, they should. Why is this not working.

Here is a link to my github files(look at the for-loop branch, not the master branch): https://github.com/sid-FBLA/GPA-Calc

You can copy & paste the code into treehouse workspaces to work with it on a live server.

Appreciate all and any help, Sid

1 Answer

Steven Parker
Steven Parker
229,783 Points

:bookmark: Hi, you asked me to answer from the previous question.

And I didn't find a "for-loop" github branch, I assume you mean the "re-Array" branch.

So after the code shown here runs, both arrays do have the same contents.

But later on, this code is on line 117:

        if(APindex[a] = index) {

This is an assignment and not a comparison, so it changes the contents of "APindex".
You probably intended to put "==" here instead of "=".

Oh my god, thank you so much, if I had known that earlier I would have saved three hours yesterday, you really are a lifesaver Steven!(yes I meant the re-array branch, I had changed it and forgot to put that in the question, sorry for any inconvenience)

Appreciate all your help, Sid