-1

I have two arrays.

    var letterGrades = ["A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D",  "D-"];
    var fall = ["Fall 2015", "A", "B", "A-"];

What I need to do is compare the two arrays and print out any values that are equal.

This is what I have tried so far...

function getGrades(semester){
if(semester === "Fall 2015"){
    for (var i = 1; i < 5; i++) {            
        for(var x = 0; x < letterGrades.length; x++){
            if(fall.data[0][i] == letterGrades[x]){
               console.log(fall.data[0][i]);
            }
        }
    }
}

getGrades("Fall 2015");

Any suggestions on how to do this? Right now, I am not getting anything returned. Thank you!

TylerYoc
  • 75
  • 2
  • 10
  • I am not well versed in js and wasn't sure exactly what that answer was accomplishing. I need to compare the two arrays and return where letterGrades"A" == fall"A" – TylerYoc Dec 19 '15 at 02:39
  • Why would someone have a grade that isn't in `letterGrades`? What's the point of this? – Barmar Dec 19 '15 at 02:39
  • What is `fall.data`? `fall` is an array, not an object, it doesn't have a `data` property. – Barmar Dec 19 '15 at 02:40
  • It should just be fall[i] I was using Papaparse to parse the fall string and then use that Json object to get the fall array. The point is, I have a third array with GPA values in it. I will need to compare the fall array with the letterGrades array and then calculate the GPA accordingly. – TylerYoc Dec 19 '15 at 02:42
  • The basic algorithm is right they syntax has a error. The fall.data[0][x] should be fall[x] – wnordmann Dec 19 '15 at 02:47
  • Why don't you use an object that maps letter grades to GPA values, `var grades = { "A": 5, "A-": 4.7, ... };` – Barmar Dec 19 '15 at 02:47
  • I would reconsider your entire methodology. What is that data tied to anyways? Why does `fall` have a `data` property. Although Arrays are also Objects in JavaScript, you would not have access to the property that way, and we've never seen the `data` property assigned. Use Objects that are not `instanceof Array`. – StackSlave Dec 19 '15 at 02:53
  • @Barmar that would probably be much easier than comparing a third array. I'm not sure of how I would compare the found values from there but I will figure it out. Thanks. – TylerYoc Dec 19 '15 at 03:02

1 Answers1

1

You're not getting anything returned because there's an error accessing the array fall. It's a 1D array but you're accessing it with fall.data[0][i]. That causes syntax error Uncaught SyntaxError: Unexpected end of input

You can loop through each element in fall starting from index 1 and in every iteration check if the current character is in letterGrades like this:

var letterGrades = ["A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D",  "D-"];
var fall = ["Fall 2015", "A", "B", "A-"];

function getGrades(semester){
    if(semester === "Fall 2015"){
        for (var i = 1; i < fall.length; i++) {            
            if (letterGrades.indexOf(fall[i]) >= 0) {
                console.log(fall[i]);
            }
        }
    }
}

getGrades("Fall 2015");
jianweichuah
  • 1,417
  • 1
  • 11
  • 22