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.

2
  • 2
    There's no such thing as a "JSON Object" Commented Sep 27, 2011 at 1: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... Commented Sep 27, 2011 at 17:05

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
@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?
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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.