From 06cbf74e61e267b564e29584c88d1bc9b7025c3c Mon Sep 17 00:00:00 2001 From: Andres Gajardo Date: Mon, 21 Aug 2017 23:43:02 -0400 Subject: [PATCH 1/4] list completed --- chapter04.js | 61 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..ad11ca5 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,37 +9,82 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + if (step == 0) { + step = 1; + } + var arr = []; + if (step >=1) { + for(var i=start; i<=end; i+=step) + arr.push(i); + } + else { + for(var i=start; i>=end; i+=step) + arr.push(i); + } + return arr; } function sum(array) { - // Your code here + var sum = 0; + for(var i=0; i= 0; i--) { + reversed.push(array[i]); + } + return reversed; } function reverseArrayInPlace(array) { - // Your code here + var mid = array.length / 2; + for(var i=0; i<=mid; i++){ + var temp = array[i]; + var opp_index = array.length-1-i; + array[i] = array[opp_index]; + array[opp_index] = temp; + } + return array; } // Problem 3: A List function arrayToList(array) { - // Your code here + var list = null; + for(var i=array.length-1; i>=0; i--){ + list = prepend(array[i], list) + } + return list; } function listToArray(list) { - // Your code here + var arr = []; + for(var node=list; node; node=node.rest) { + arr.push(node.value); + } + return arr; } function nth(list, position) { - // Your code here + var count = 0; + for(var node=list; node; node=node.rest) { + if(count == position) + return node.value; + count ++; + } + return undefined; } function prepend(element, list) { - // Your code here + var new_list = { + 'value': element, + 'rest': list + } + return new_list; } // Problem 4: Deep comparison From a534ffd0650c7c4ce283b28597e9f30c604ab16b Mon Sep 17 00:00:00 2001 From: Andres Gajardo Date: Tue, 22 Aug 2017 01:00:31 -0400 Subject: [PATCH 2/4] Chapter 4 done --- chapter04.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index ad11ca5..0d69bf5 100644 --- a/chapter04.js +++ b/chapter04.js @@ -89,7 +89,29 @@ function prepend(element, list) { // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + //Check if identical + if (obj1 === obj2) { + return true; + } + + //Check type and nulls + if (obj1 == null || obj2 == null || typeof(obj1) != "object" || typeof(obj2) != "object") { + return false; + } + + //Check length + if (Object.keys(obj1).length != Object.keys(obj2).length) { + return false; + } + //Deep comparison + for(item in obj1) { + if (obj2.hasOwnProperty(item)) { + if ( !(deepEqual(obj1[item], obj2[item]))) { + return false; + } + } + } + return true; } From 055235a955e83a189ff3273e265166ddb4a63214 Mon Sep 17 00:00:00 2001 From: Andres Gajardo Date: Thu, 7 Sep 2017 13:26:37 -0400 Subject: [PATCH 3/4] Complete CH5, used ES6 features --- chapter05.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/chapter05.js b/chapter05.js index d1372cb..5f61852 100644 --- a/chapter05.js +++ b/chapter05.js @@ -8,25 +8,32 @@ const ancestry = require('./ancestry'); function average(array) { - function plus(a, b) { return a + b; } - return array.reduce(plus) / array.length; + function plus(a, b) { return a + b; } + return array.reduce(plus) / array.length; } const byName = {}; -ancestry.forEach(function(person) { - byName[person.name] = person; +ancestry.forEach((person) => { + byName[person.name] = person; }); // Problem 1: Flattening -function flatten(arrays) { - // Your code here +function flatten(array) { + return array.reduce((flat_arr, curr_arr) => { + return flat_arr.concat(curr_arr); + }, []) } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { - // Your code here + let differences = ancestry.filter((person) => { + return byName[person.mother] != null; //filter those who only have mothers in data + }).map((person) => { + return person.born - byName[person.mother].born; + }); + return average(differences); } // Problem 3: Historical life expectancy @@ -34,7 +41,18 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + let centuryAges = {}; + ancestry.forEach((person) => { + let century = Math.ceil(person.died / 100); + if (!(centuryAges.hasOwnProperty(century))) { + centuryAges[century] = []; + } + centuryAges[century].push(person.died-person.born); + }); + for (century in centuryAges) { + centuryAges[century] = average(centuryAges[century]); + } + return centuryAges; } From 746357a29dba4bf68042f0ed8985d4e42f759a78 Mon Sep 17 00:00:00 2001 From: Andres Gajardo Date: Thu, 7 Sep 2017 15:26:36 -0400 Subject: [PATCH 4/4] Pass all tests --- .eslintrc | 0 chapter04.js | 38 +++++++++++++++++++------------------- chapter13.js | 23 ++++++++++++++++++++++- 3 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 .eslintrc diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e69de29 diff --git a/chapter04.js b/chapter04.js index 0d69bf5..a3be62c 100644 --- a/chapter04.js +++ b/chapter04.js @@ -12,40 +12,40 @@ function range(start, end, step=1) { if (step == 0) { step = 1; } - var arr = []; + let arr = []; if (step >=1) { - for(var i=start; i<=end; i+=step) + for(let i=start; i<=end; i+=step) arr.push(i); } else { - for(var i=start; i>=end; i+=step) + for(let i=start; i>=end; i+=step) arr.push(i); } return arr; } function sum(array) { - var sum = 0; - for(var i=0; i= 0; i--) { + let reversed = []; + let last = array.length - 1 + for(let i = last; i >= 0; i--) { reversed.push(array[i]); } return reversed; } function reverseArrayInPlace(array) { - var mid = array.length / 2; - for(var i=0; i<=mid; i++){ - var temp = array[i]; - var opp_index = array.length-1-i; + let mid = array.length / 2; + for(let i=0; i<=mid; i++){ + let temp = array[i]; + let opp_index = array.length-1-i; array[i] = array[opp_index]; array[opp_index] = temp; } @@ -54,24 +54,24 @@ function reverseArrayInPlace(array) { // Problem 3: A List function arrayToList(array) { - var list = null; - for(var i=array.length-1; i>=0; i--){ + let list = null; + for(let i=array.length-1; i>=0; i--){ list = prepend(array[i], list) } return list; } function listToArray(list) { - var arr = []; - for(var node=list; node; node=node.rest) { + let arr = []; + for(let node=list; node; node=node.rest) { arr.push(node.value); } return arr; } function nth(list, position) { - var count = 0; - for(var node=list; node; node=node.rest) { + let count = 0; + for(let node=list; node; node=node.rest) { if(count == position) return node.value; count ++; @@ -80,7 +80,7 @@ function nth(list, position) { } function prepend(element, list) { - var new_list = { + let new_list = { 'value': element, 'rest': list } diff --git a/chapter13.js b/chapter13.js index ab99bc6..95f104d 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,7 +9,28 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + let table = document.createElement("table"); + let attributes = Object.keys(data[0]); + let top_row = document.createElement("tr"); + for (attrib in attributes) { + let header_cell = document.createElement('th'); + header_cell.textContent = attrib; + top_row.appendChild(header_cell); + } + table.appendChild(top_row); + data.forEach((obj) => { //Each mountain object + let table_row = document.createElement("tr"); + attributes.forEach((attrib) => { //Name, Height, Country values + let row_cell = document.createElement("td"); + row_cell.textContent = obj[attrib]; + if (typeof obj[attrib] == "number") { + row_cell.style.textAlign == "right"; + } + table_row.appendChild(row_cell); + }); + table.appendChild(table_row); + }); + return table; }