diff --git a/chapter04.js b/chapter04.js index 12f6ef0..9e43aa8 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,42 +9,127 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + var array = []; + var start_cpy = start; + + for(var i=0; i<=(end-start); i=i+step){ + array[i] = start_cpy; + start_cpy = start_cpy+1; + } + return array; } function sum(array) { - // Your code here + var sum = 0; + var old_sum = 0; + for(var i=0; i= 0; i--) { + if(ancestry_array[i].name === entry.mother){ + return entry.born - ancestry_array[i].born + } + } + }); + + var null_filter = _.filter(age_differences,function(entry){ + if(entry != null){ + return true; + } + else{ + return false; + } + }); + + var final_avg = average(null_filter); + return final_avg; } // Problem 3: Historical life expectancy /* This must return the object/map with centuries as keys and average age for the century as the value */ + function averageAgeByCentury() { - // Your code here + var ancestry_array = JSON.parse(ancestry); + + var deaths_centuries = _.map(ancestry_array,function(entry){ + var new_obj = {} + new_obj["age_at_death"] = entry.died - entry.born; + new_obj["century"] = Math.ceil(entry.died/100); //rounds up using ceiling + return new_obj; + }); + + var avgs = []; + var output_object = {}; + for(var i=16; i<22; i++){ + var ith_filter = _.filter(deaths_centuries,function(entry){ + if(entry["century"]===i){ + return true; + } + }); + + var ith_map = _.map(ith_filter,function(entry){ + return entry.age_at_death; + }); + + var avg = average(ith_map); + avgs.push(avg); + output_object[i]=avg; + } + return output_object; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..ca290e1 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,7 +9,28 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + var output = "\n"; + var properties = Object.keys(data[0]); + var n = properties.length; + + output += "\t\n"; + for(var i=0; i < n; i++){ + output += "\t\t\n"; + } + output += "\n\t"; + + for(var i=0; i\n"; + } + output+= "\n\t"; + } + output+="\n
" + properties[i] + "
"; + + return output; }