From b7e1b4b7f78dcc898c5d13bf6c6314a5d993229f Mon Sep 17 00:00:00 2001 From: David Hadaller Date: Sat, 2 Sep 2017 22:24:12 -0400 Subject: [PATCH 1/2] adding my work --- chapter04.js | 84 +++++++++++++++++++++++++++++++++++++++++++++++----- chapter05.js | 63 +++++++++++++++++++++++++++++++++++++-- chapter13.js | 23 +++++++++++++- 3 files changed, 158 insertions(+), 12 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..b58fbba 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,37 +9,105 @@ // 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; } From 9552e060e1389ff98a1d8d4f037bc506620769a4 Mon Sep 17 00:00:00 2001 From: David Hadaller Date: Sat, 2 Sep 2017 22:48:28 -0400 Subject: [PATCH 2/2] added the deep equal function to ch. 4 --- chapter04.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index b58fbba..9e43aa8 100644 --- a/chapter04.js +++ b/chapter04.js @@ -112,7 +112,24 @@ function prepend(element, list) { // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + if( (typeof obj1 === typeof obj2) && typeof obj1 != "object" ){ + if(obj1===obj2)return true; + else return false; + } + + else if((typeof obj1 === typeof obj2) && typeof obj1 === object){ + var obj1_keys = Object.keys(obj1); + var obj2_keys = Object.keys(obj2); + + //testing to see if they have the same keys + for(var i=0; i