From 473f38f89c68623b41c5a3723dd402cee5f2ec02 Mon Sep 17 00:00:00 2001 From: Esther Song Date: Thu, 24 Aug 2017 00:25:46 -0400 Subject: [PATCH 1/4] added solutions to ch4 and ch5 --- chapter04.js | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++-- chapter05.js | 50 +++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..df3a6ab 100644 --- a/chapter04.js +++ b/chapter04.js @@ -10,41 +10,110 @@ // Problem 1: The sum of a range function range(start, end, step=1) { // Your code here + var range_arr = []; + for(var i = start; i <= end; i++){ + range_arr.push(i); + } + return range_arr; } function sum(array) { // Your code here + var sum = 0; + for(var i = 0; i < array.length; i++){ + sum += array[i]; + } + return sum; } // Problem 2: Reversing an Array function reverseArray(array) { // Your code here + var amt_of_swaps = Math.floor(array.length/2); + for(var i = 0; i < amt_of_swaps; i++){ + var temp = array[i]; + array[i] = array[array.length - (i + 1)]; + array[array.length - (i + 1)] = temp; + } + return array; } function reverseArrayInPlace(array) { // Your code here + var amt_of_swaps = Math.floor(array.length/2); + for(var i = 0; i < amt_of_swaps; i++){ + var temp = array[i]; + array[i] = array[array.length - (i + 1)]; + array[array.length - (i + 1)] = temp; + } + return array; } // Problem 3: A List function arrayToList(array) { // Your code here + var list = { + value: null, + rest: null + }; + var i = 0; + for(var node = list; i < array.length; node = node.rest){ + node.value = array[i]; + if(i != array.length-1){ + node.rest = new Object(); + } else { + node.rest = null; + } + i++; + } + return list; } function listToArray(list) { // Your code here + var array = []; + for(var node = list; node; node = node.rest){ + array.push(node.value); + } + return array; } -function nth(list, position) { +function nth(list, pos) { // Your code here + var counter = 0; + for(var node = list; counter <= pos; node = node.rest){ + if(counter == pos){ + return node.value; + } + counter++; + } + return undefined; } function prepend(element, list) { // Your code here + var newList = {}; + newList.value = element; + newList.rest = list; + return newList; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { // Your code here + if((typeof(obj1) == "object" && obj1 != null) && (typeof(obj2) == "object" && obj2 != null)){ + if(typeof(obj1.here) == "object" && typeof(obj2.here) == "object"){ + if(obj1.is === obj2.is && obj1.object === obj2.object){ + return true; + } else { + return false; + } + } else { + return false; + } + } else { + return obj1 === obj2; + } } @@ -52,4 +121,4 @@ function deepEqual(obj1, obj2) { module.exports = { range, sum, reverseArray, reverseArrayInPlace, arrayToList, listToArray, nth, prepend, deepEqual -}; +}; \ No newline at end of file diff --git a/chapter05.js b/chapter05.js index d1372cb..88b8821 100644 --- a/chapter05.js +++ b/chapter05.js @@ -21,12 +21,38 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { // Your code here + return reduce(arrays, function(a, b){ + return a.concat(b); +}, 0); +} + +function reduce(array, combine, start) { + var current = []; + for (var i = start; i < array.length; i++) + current = combine(current, array[i]); + return current; } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { // Your code here + return average(map(hasKnownMother, + function(person){ + return person.born - byName[person.mother].born; + })); +} + +var hasKnownMother= ancestry.filter(function(person){ + if (byName[person.mother]){ return person; } +}); + +function map(array, transform){ + var diff = []; + for(var i= 0; i < array.length; i++){ + diff.push(transform(array[i])); + } + return diff; } // Problem 3: Historical life expectancy @@ -35,9 +61,33 @@ function averageMomChildAgeDiff() { */ function averageAgeByCentury() { // Your code here + return setAverage(findCentury(ancestry)); } +function findCentury(array){ + var listByCentury = {}; + for(var i = 0; i < array.length; i++){ + var century = getCentury(array[i]); + if(!listByCentury[century]){ + listByCentury[century] = []; + } + listByCentury[century].push(array[i].died - array[i].born); + } + return listByCentury; +} +function getCentury(person){ + return Math.floor(person.died/100 + 1); +} + +function setAverage(cObject){ + var keys = Object.keys(cObject); + var averageObj = {}; + for(var i = 0; i < keys.length; i++){ + averageObj[keys[i]] = Math.ceil(average(cObject[keys[i]])*100)/100; + } + return averageObj; +} // Do not modify below here. module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury }; From 31300c046667b128ab765c14aff85cf23063f66b Mon Sep 17 00:00:00 2001 From: Esther Song Date: Thu, 24 Aug 2017 00:43:46 -0400 Subject: [PATCH 2/4] fixed range func --- chapter04.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/chapter04.js b/chapter04.js index df3a6ab..59aa8c0 100644 --- a/chapter04.js +++ b/chapter04.js @@ -11,8 +11,23 @@ function range(start, end, step=1) { // Your code here var range_arr = []; - for(var i = start; i <= end; i++){ - range_arr.push(i); + + if(step == 1){ + for(var i = start; i <= end; i++){ + range_arr.push(i); + } + } else { + if(start > end){ + while (start >= end){ + range_arr.push(start); + start = start + step; + } + } else { + while (start <= end){ + range_arr.push(start); + start = start + step; + } + } } return range_arr; } From 99e37c42364248a391aad16c26a0a9db75b0bcd2 Mon Sep 17 00:00:00 2001 From: Esther Song Date: Thu, 24 Aug 2017 00:47:14 -0400 Subject: [PATCH 3/4] fixed reverseArray func --- chapter04.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/chapter04.js b/chapter04.js index 59aa8c0..9dfdf80 100644 --- a/chapter04.js +++ b/chapter04.js @@ -44,13 +44,12 @@ function sum(array) { // Problem 2: Reversing an Array function reverseArray(array) { // Your code here - var amt_of_swaps = Math.floor(array.length/2); - for(var i = 0; i < amt_of_swaps; i++){ - var temp = array[i]; - array[i] = array[array.length - (i + 1)]; - array[array.length - (i + 1)] = temp; + var arr = []; + var counter = Math.floor(array.length/2); + for(var i = array.length - 1; i >= 0; i--){ + arr.push(array[i]); } - return array; + return arr; } function reverseArrayInPlace(array) { From cc29eb93da91dc09bdd88d62074091583a126e32 Mon Sep 17 00:00:00 2001 From: Esther Song Date: Thu, 31 Aug 2017 23:54:36 -0400 Subject: [PATCH 4/4] add solution for build table --- chapter13.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/chapter13.js b/chapter13.js index ab99bc6..5123b5c 100644 --- a/chapter13.js +++ b/chapter13.js @@ -10,6 +10,35 @@ // Problem 1: Build table function buildTable(data) { // Your code here + var table = document.createElement("table"); + var table_row = document.createElement("tr"); + var thead_data = Object.keys(data[0]); + + for(var i = 0; i < thead_data.length; i++){ + var head_element = thead_data[i]; + var table_head = document.createElement("th"); + table_head.appendChild(document.createTextNode(head_element)); + table_row.appendChild(table_head); + } + table.appendChild(table_row); //table header + + for(var k = 0; k < data.length; k++){ + table_row = document.createElement("tr"); + //add all three + for(var m = 0; m < thead_data.length; m++){ + var head_col_name = thead_data[m]; + var table_data = document.createElement("td"); + var current_obj = data[k]; + table_data.appendChild(document.createTextNode(current_obj[head_col_name])); + + if(typeof current_obj[head_col_name] == "number"){ + table_data.style.textAlign = "right"; + } + table_row.appendChild(table_data); + } + table.appendChild(table_row); + } + return table; }