This function returns the total from an array, and it works perfectly. But if i initialize total inside the for loop, it does not return the sum. Can you please tell me why?
function sum(arr) {
    var total=0;
    for (var i=0;i<arr.length;i++){
        total += arr[i];
    }
    return total;
}