0

So I'm trying to create a function that takes in an array (I guess it's more of a JSON object, or so we were told) object and returns a value based on that array, but I keep getting an error, so I'm pretty certain I'm doing this wrong.

I'm fairly new at JavaScript so go easy on me. Also, I found this thread which is similar to the question I'm asking, but I don't quite understand THAT question (and therefore it's answers).

Here's a sample of the object we're given:

var returned_json =  {
    "nike_runs": [
        {
            "start_time": "2011-03-11T19:14:44Z",
            "calories": 12.0,
            "distance_miles": "0.10",
            "total_seconds": 288.0,
            "average_pace":"50.47"
        },
        {
            "start_time": "2011-03-11T19:41:25Z",
            "calories": 7.0,
            "distance_miles": "0.06",
            "total_seconds": 559.0,
            "average_pace": "165.19"
        },
        {
            "start_time": "2011-03-11T20:27:45Z",
            "calories": 197.0,
            "distance_miles": "1.63",
            "total_seconds": 8434.0,
            "average_pace": "86.22"
        },
        ...
    ]
}

Here's my code:

function getExp (returned_json) {
    var exp;
    for (var i = 0; i <= returned_json.nike_runs.length; i++) {
        exp += returned_json.nike_runs[i].calories;
    }
    return exp;
}

It returns an error:

TypeError: returned_json.nike_runs[i] is undefined

I figured this has to do with the fact I'm not defining the type of object I want to pass into the function, but my research tells me that doesn't matter.

Help? :(

Thanks.

Community
  • 1
  • 1
Eddie Rivas
  • 335
  • 1
  • 3
  • 11
  • 2
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Matt Ball Sep 27 '11 at 01:25
  • 1
    Mmm, thanks, this is actually pretty useful. Sorry for the misconception, that's what I was TOLD it was. Can't believe everything you hear, huh... – Eddie Rivas Sep 27 '11 at 17:05

1 Answers1

5

Use i < returned_json.nike_runs.length, not i <= returned_json.nike_runs.length.

Edit: While you’re at it, you better define a starting value for exp too.

Daniel Brockman
  • 18,826
  • 3
  • 29
  • 40
  • actually for preformance I would use `var i = 0, l = returned_json.nike_runs.length; i < l; i++` then you are only collecting the length of the object once instead of each itteration. FYI. – rlemon Sep 27 '11 at 01:25
  • @rlemon I see what you're saying about the collection, but that operation is so minimal that ultimately it may not even make a difference, but you're right, so I'll do it. – Eddie Rivas Sep 27 '11 at 16:53
  • @Daniel Brockman: Thanks man, your advice helped, as soon as I read it it was obvious where my mistake was, I don't understand how that returned a Type error though, but whatever -shrugs- However, after fixing it I got another error... My getExp(returned_json) operation returns 0. FML. Any ideas why? – Eddie Rivas Sep 27 '11 at 16:59
  • Nevermind, all fixed. Daniel, thanks a bunch, I'm a freakin' retard that obviously can't notice super obvious, CS101 bugs. I feel so stupid T_T Thanks man. – Eddie Rivas Sep 27 '11 at 17:01
  • @DanielBrockman Yeah dude, thanks a bunch. It was such a simple fix that I felt like a dumbass. Oh bugs, they're such little a-holes. – Eddie Rivas Sep 29 '11 at 19:13