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);
}
}