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

Why does this not work?

In my code this statement:

console.log(APindex[index]);

Here is a snapshot of my code: https://w.trhou.se/cou06fq2dp

Lines 73-121 are important, you can ignore the rest.

1 Answer

Steven Parker
Steven Parker
229,732 Points

As I mentioned in the previous question, an item is only pushed onto the "APIndex" array when the GPA is "AP", but an attempt to reference an item at "APIndex[index]" happens for every second "subarray" item (i == 1).

Did you perhaps put "APIndex" accidentally where you meant to use "subarray"?

Okay, I kind of understand what you are saying but I still don't really get it. Here's what's going on:

There is an array called "convertToLetter" the array is organized in this manner:

classDifficulty can be "AP" or "HONORS" or "CP" letterGrade is just the letter grade such as "A" "A-" 'B+" and so on.

[classDifficulty][letterGrade] [classDifficulty][letterGrade] [classDifficulty][letterGrade] [classDifficulty][letterGrade] [classDifficulty][letterGrade] [classDifficulty][letterGrade] [classDifficulty][letterGrade] this can occur one more time, depending on the number of classes that the student enters.

I have this new variable called "classGPA" which is .map callback called on the array "convertToLetter" this callback takes in two parameters one of which is subarray, which is each "array" block (meaning [classDifficulty][letterGrade]), it also takes in the counter parameter index, whose value goes from 0 to the number of "array blocks." I made another .map callback on the "classGPA" called subarray.map to access either the [classDifficulty] or the [letterGrade], and I called the counter parameter 'i' on this function.

When i === 0 we are accessing the [classDifficulty]. When i === 1 we are accessing the [letterGrade].

From here I do this

if (i === 0) {
      if(GPA.toUpperCase() == "AP") {
        APindex.push(index);
      } else if(GPA.toUpperCase() == "HONORS") {
        HONORSindex.push(index);
      } else if(GPA.toUpperCase() == "CP") {
        CPindex.push(index);
      }
    }

if (i === 1) {
      console.log(index);
      console.log(APindex[index]);

(I ended the if statement there is just more code underneath)

So the APindex array gets all indexes at which the classDifficulty = "AP" Shouldn't this mean that by logging APindex[index] I get the value at which the index counter equals to APindex(only if user doesn't put in an AP class should it return undefined).

I have the code on github now: https://github.com/sid-FBLA/GPA-Calc/blob/master/map.js

I know that this doesn't necessarily relate to my question but here's what I'm trying to do with the classGPA

I want an array with the: [classDifficulty][GPAvalue]

GPAvalue refers to the GPA I called for the function to return you can take a look at this on the code, starting from line 91.

For example console.log(classGPA) should return something like this(obviously classDifficulty and GPAvalue can vary based on the user input, but you get the idea, instead it returns undefined everytime for the GPAvalue, unless all the classes are AP, HONORS, or CP).

["AP"][5.00] ["CP"][4.00] ["AP"][4.66] ["AP"][4.00] ["AP"][5.00] ["AP"][5.00] ["HONORS"][4.5] ["AP"][5.00]

Thanks so much for helping, Sid

Steven Parker
Steven Parker
229,732 Points

So, using your example list, when the index is 0, then GPA is "AP" and an item is pushed onto APIndex. That means APindex[index] exists since it has one item (index 0).

But on the next step, when the index is 1, then GPA is "CP", so no item is pushed onto APIndex leaving it still having one item. But now APindex[index] does not exist since it has no item at index 1. This gives "undefined".

At that point, all remaining items will return "undefined" since even if another item is pushed into the APindex array, the last item index will still be less than "index".

Beyond just fixing the bug, you might want to rethink how your implementation leads to the objective. With a revised code strategy you might be able to reduce the number of steps and/or the complexity of the data structures considerably.

And github is good for sharing source code, but the workspace allows the program to be easily run and tested.

Thank you for your patience and explanations, but I'm still not quite understanding the problem, I have three different 'if statements' that check if index = AP[index]/HONORSindex[index]/CPindex[index], so if index does not equal APindex[index], then it'll just check if index = HONORSindex[index] if that is also not true it will check if index = CPindex[index]. One of these statements will have to be true, and the function should just run on, why is this not working?

I get this part of your answer:

So, using your example list, when the index is 0, then GPA is "AP" and an item is pushed onto APIndex. That means >APindex[index] exists since it has one item (index 0).

But on the next step, when the index is 1, then GPA is "CP", so no item is pushed onto APIndex leaving it still having one >item. But now APindex[index] does not exist since it has no item at index 1. This gives "undefined".

What confuses me is this next line:

At that point, all remaining items will return "undefined" since even if another item is pushed into the APindex array, the >last item index will still be less than "index".

One of the items is undefined, so why would that make the rest of the items return undefined?

Please respond as an answer, I really appreciate your help and would like to mark your response to this as "Best Answer." Thanks for the help once again, Sid

Steven Parker
Steven Parker
229,732 Points

If we continue the sequence to the third step when the index is 2, it no longer matters what GPA is. Because if it is not "AP", the array will still have just one item. And if it is "AP", a new item is pushed giving APIndex two items. But since "index" is now 2, APindex[index] still does not exist and returns "undefined".

The point is that once the value of "index" becomes equal to or greater than the number of items in the APindex array, it doesn't matter what the other items contain, the size of APindex will never "catch up" to the value of "index", so all remaining items will return "undefined".

Thanks for the help, I get it now, I know how to fix it, but I'm having another issue that I would really appreciate your help with, here is a link to the question: https://teamtreehouse.com/community/push-method-is-not-working-correctly