0

I have a function with a loop that calls (If the conditions are true) to another function that .push() it returning data into an array as an object. what's happening is that after the first call to the second function the loop from the first function is stop and the code moves on to the next part of code (No errors in chrome console and I get the right object pushed). What is the problem?

First function:

    if (window.localStorage.length > 1) {
        track_items_for_chart = [];
        for (i = 0; i < window.localStorage.length; i++) {
            console.log(i);
            var key_name = (window.localStorage).key(i);
            var record_time = (window.localStorage.getItem(key_name));
            if (record_time !== '[]') {
                console.log("record_time !== '[]'");
                if (key_name !== 'exp') {
                    console.log("key_name !== 'exp'");
                    if (key_name !== 'ripple-last-load') {
                        console.log("key_name !== 'ripple-last-load'");
                var computed_info = get_total_km(key_name);
                    }

                }


            }
        }

Second function:

function get_total_km($object_key) {

    // Get all the GPS data for the specific workout
    var data = window.localStorage.getItem($object_key);

    // Turn the stringified GPS data back into a JS object
    data = jQuery.parseJSON(data);
    if (data) {
        // Calculate the total distance travelled
        total_km = 0;
        for (i = 0; i < data.length; i++) {

            if (i === (data.length - 1)) {
                break;
            }

            total_km += gps_distance(data[i].coords.latitude, data[i].coords.longitude, data[i + 1].coords.latitude, data[i + 1].coords.longitude);
        }

        total_km_rounded = parseFloat(total_km.toFixed(2));
        // Calculate the total time taken for the track
        start_time = new Date(data[0].timestamp).getTime();
        end_time = new Date(data[data.length - 1].timestamp).getTime();
        total_time_ms = end_time - start_time;
        total_time_s = total_time_ms / 1000;
        final_time_m = Math.floor(total_time_s / 60);
        final_time_s = Math.floor(total_time_s - (final_time_m * 60));
//        console.log({total_km_rounded: total_km_rounded, final_time_m: final_time_m, final_time_s: final_time_s});
        var time_mas = parseFloat(final_time_m + "." + final_time_s);
        track_items_for_chart.push(total_km_rounded, time_mas);
    }
}
Dori
  • 331
  • 1
  • 6
  • 18
  • You have this line: `var computed_info = get_total_km(key_name);` but you never actually return a value from `get_total_km`. – Steve Nov 08 '13 at 05:16
  • I do not want to return nothing. But anyway I have added return; to the second function and still its the same... – Dori Nov 08 '13 at 05:23

1 Answers1

1

ּStart with for( var i =0) in both functions. This is little confusing if you used to another languages, but JS indicates all the i (until you defined them with var) as the same argument. so after the second function run, i is already bigger than your localstorage length.

You need to read more about JS arguments scope;

maybe start here: What is the scope of variables in JavaScript?

Community
  • 1
  • 1
anysite
  • 148
  • 1
  • 12
  • e.g. That's little more complected than "JS indicates all the i as the same argument". Extremely not always, but true for this code. – anysite Nov 08 '13 at 06:26
  • i meant i knew it but didn't spot it, anyhow i have no idea what complected mean; never mind, +1 from me – mikakun Nov 08 '13 at 06:29