From 9b765dc4be89ffb5002b0d2d1861b9e613de5545 Mon Sep 17 00:00:00 2001 From: Yaocheng Wu Date: Fri, 1 Sep 2017 15:04:54 -0400 Subject: [PATCH 1/2] finished all assignments --- chapter04.js | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++- chapter05.js | 30 +++++++++++++++++++ chapter13.js | 22 ++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..9acbc26 100644 --- a/chapter04.js +++ b/chapter04.js @@ -10,41 +10,122 @@ // Problem 1: The sum of a range function range(start, end, step=1) { // Your code here + let arr = []; + + if (step > 0) { + for (let i = start; i <= end; i += step) { + arr.push(i); + } + } + + if (step < 0) { + for (let i = start; i >= end; i += step) { + arr.push(i); + } + } + + return arr; + } function sum(array) { // Your code here + let sum = 0; + array.forEach((x) => { sum += x;}); + + return sum; } // Problem 2: Reversing an Array function reverseArray(array) { // Your code here + let arr = []; + const arrayLength = array.length; + + for (let i = arrayLength - 1; i >= 0; i -= 1) { + arr.push(array[i]); + } + + return arr; } function reverseArrayInPlace(array) { // Your code here + const arrLength = array.length; + for (let s = 0, e = arrLength-1; s < e;) { + let a = array[s]; + array[s] = array[e]; + array[e] = a; + + s += 1; + e -= 1; + } + + return array; } // Problem 3: A List function arrayToList(array) { // Your code here + var v = array[0]; + array.splice(0, 1); + + if (array.length === 0) { + return {value: v, rest: null}; + } + + return {value: v, rest: arrayToList(array)} } function listToArray(list) { // Your code here + var arr = []; + while (list !== null) { + arr.push(list.value) + list = list.rest; + } + + return arr; } function nth(list, position) { // Your code here + if (list === null) { + return null; + } + if (position === 0) { + return list.value; + } + + list = list.rest; + position -= 1; + return nth(list, position); } function prepend(element, list) { // Your code here + var head = {value: element, rest: list}; + + return head; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { // Your code here + if ((typeof(obj1) === "object" && typeof(obj1) !== null) && (typeof(obj2) === "object" && typeof(obj2) !== null)) { + for (var x in obj1) { + if (obj2.hasOwnProperty(x)) { + if (!deepEqual(obj1[x], obj2[x])) { + return false; + } + } else { + return false; + } + } + } else if (obj1 !== obj2) { + return false; + } + return true; } @@ -52,4 +133,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..1430bde 100644 --- a/chapter05.js +++ b/chapter05.js @@ -21,12 +21,26 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { // Your code here + var newArray = arrays.reduce((x, y) => {return x.concat(y);}); + return newArray; } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { // Your code here + var ageDiffs = [] + for (person in byName) { + var child = byName[person]; + if (child.mother) { + var mother = byName[child.mother]; + if (mother) { + ageDiffs.push(child.born - mother.born); + } + } + } + + return average(ageDiffs); } // Problem 3: Historical life expectancy @@ -35,6 +49,22 @@ function averageMomChildAgeDiff() { */ function averageAgeByCentury() { // Your code here + var ageGroups = {}; + for (person in byName) { + var century = Math.ceil(byName[person].died / 100); + var personAge = byName[person].died - byName[person].born + if (ageGroups[century]) { + ageGroups[century].push(personAge); + } else { + ageGroups[century] = [personAge]; + } + } + + for (group in ageGroups) { + ageGroups[group] = average(ageGroups[group]); + } + + return ageGroups; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..2a713fe 100644 --- a/chapter13.js +++ b/chapter13.js @@ -10,6 +10,28 @@ // Problem 1: Build table function buildTable(data) { // Your code here + var table = document.createElement('table'); + var headerRow = document.createElement('tr'); + + for (header in data[0]) { + var th = document.createElement('th'); + th.textContent = header; + headerRow.appendChild(th); + } + table.appendChild(headerRow); + + data.forEach(d => { + var row = document.createElement('tr'); + + for (prop in d) { + var td = document.createElement('td'); + td.textContent = d[prop]; + row.appendChild(td); + } + table.appendChild(row); + }); + + return table; } From abbf172131509bd61ad2bc1517b3edd280594d89 Mon Sep 17 00:00:00 2001 From: Yaocheng Wu Date: Fri, 1 Sep 2017 15:09:11 -0400 Subject: [PATCH 2/2] added newline at eof --- chapter04.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index 9acbc26..322ecd6 100644 --- a/chapter04.js +++ b/chapter04.js @@ -133,4 +133,4 @@ function deepEqual(obj1, obj2) { module.exports = { range, sum, reverseArray, reverseArrayInPlace, arrayToList, listToArray, nth, prepend, deepEqual -}; \ No newline at end of file +};