From 7229b5d419cb13c470ca2f11f31fca1bf113755d Mon Sep 17 00:00:00 2001 From: tanight Date: Wed, 30 Aug 2017 14:48:37 -0400 Subject: [PATCH] EloquentJS HW Submission --- chapter04.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- chapter05.js | 29 +++++++++++++++++++++++++++++ chapter13.js | 22 ++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..d26fab6 100644 --- a/chapter04.js +++ b/chapter04.js @@ -10,41 +10,89 @@ // Problem 1: The sum of a range function range(start, end, step=1) { // Your code here + var array = []; + for (var i = start; step > 1 || step === undefined ? i <= end : i >= end; step ? i = i + step : i++) + array.push(i); + return array; } function sum(array) { // Your code here + var result = 0; + for(var i=0; i= 0; i--) + newArray.push(array[i]); + return newArray; } function reverseArrayInPlace(array) { // Your code here + var index = 0; + for (var i = 0; i < array.length / 2; i++) { + index = array[i]; + array[i] = array[array.length - i - 1]; + array[array.length - i - 1] = index; + } } +console.log(reverseArray(["A", "B", "C"])); +// ? ["C", "B", "A"]; +var arrayValue = [1, 2, 3, 4, 5]; +reverseArrayInPlace(arrayValue); +console.log(arrayValue); +// ? [5, 4, 3, 2, 1] + // Problem 3: A List function arrayToList(array) { // Your code here + var list = null; + for (var i = array.length - 1; i >= 0; i--) + list = {value: array[i], rest: list}; + 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) { // Your code here + if (!list) + return undefined; + else if (position == 0) + return list.value; + else + return nth(list.rest, position - 1); } function prepend(element, list) { // Your code here + return {value: value, rest: list}; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + // Your code heres } diff --git a/chapter05.js b/chapter05.js index d1372cb..007ee7f 100644 --- a/chapter05.js +++ b/chapter05.js @@ -21,12 +21,19 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { // Your code here + console.log(arrays.reduce(function(flat, current) { + return flat.concat(current); } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { // Your code here + var differences = ancestry.filter(function(person) { + return byName[person.mother] != null; + }).map(function(person) { + return person.born - byName[person.mother].born; + }); } // Problem 3: Historical life expectancy @@ -35,6 +42,28 @@ function averageMomChildAgeDiff() { */ function averageAgeByCentury() { // Your code here + function groupBy(array, groupOf) { + var groups = {}; + array.forEach(function(element) { + var groupName = groupOf(element); + if (groupName in groups) + groups[groupName].push(element); + else + groups[groupName] = [element]; + }); + return groups; + } + + var byCentury = groupBy(ancestry, function(person) { + return Math.ceil(person.died / 100); + }); + + for (var century in byCentury) { + var ages = byCentury[century].map(function(person) { + return person.died - person.born; + }); + console.log(century + ": " + average(ages)); + } } diff --git a/chapter13.js b/chapter13.js index ab99bc6..959b8df 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 fields = Object.keys(data[0]); + var headRow = document.createElement("tr"); + fields.forEach(function(field) { + var headCell = document.createElement("th"); + headCell.textContent = field; + headRow.appendChild(headCell); + }); + table.appendChild(headRow); + data.forEach(function(object) { + var row = document.createElement("tr"); + fields.forEach(function(field) { + var cell = document.createElement("td"); + cell.textContent = object[field]; + if (typeof object[field] == "number") + cell.style.textAlign = "right"; + row.appendChild(cell); + }); + table.appendChild(row); + }); + return table; }