diff --git a/chapter04.js b/chapter04.js index 12f6ef0..42670d7 100644 --- a/chapter04.js +++ b/chapter04.js @@ -10,41 +10,129 @@ // Problem 1: The sum of a range function range(start, end, step=1) { // Your code here + var numRange = []; + if(start <= end) { + + for(var i = start; i <= end; i+=step) { + numRange.push(i); + } + } + else { + for(var j = start; j >= end; j+=step){ + numRange.push(j); + } + } + return numRange; } function sum(array) { // Your code here + var total = 0; + for(var i = 0; i < array.length; ++i) { + total += array[i]; + } + return total; } // Problem 2: Reversing an Array function reverseArray(array) { // Your code here + var revArr = []; + for(var i = array.length - 1; i > -1; --i) { + revArr.push(array[i]); + } + return revArr; } function reverseArrayInPlace(array) { // Your code here + var temp = 0; + for(var i = 0; i < array.length / 2; ++i) { + temp = array[i]; + array[i] = array[array.length - 1 - i]; + array[array.length - 1 - i] = temp; + } } // Problem 3: A List function arrayToList(array) { // Your code here + var list = {}; + var node = list; + if(array.length > 0) { + list = { + value: array[0], + rest: {} + }; + node = list; + } + + for(var i = 1; i < array.length; ++i) { + if(i != array.length - 1){ + node.rest = { + value: array[i], + rest: {} + }; + } + else { + node.rest = { + value: array[i], + rest: null + }; + } + node = node.rest; + } + + return list; } function listToArray(list) { // Your code here + var array = []; + + var ptr = list; + while(ptr != null) { + array.push(ptr.value); + ptr = ptr.rest; + } + return array; } function nth(list, position) { // Your code here + var ptr = list; + for(var i = 0; i < position; ++i) { + if(ptr.rest === null) { + return undefined; + } + else { + ptr = ptr.rest; + } + } + return ptr.value; } function prepend(element, list) { // Your code here + var newList = { + value: element, + rest: list + } + return newList; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { // Your code here + for(var prop in obj1) { + if(typeof obj1[prop] == 'object' && typeof obj2[prop] == 'object') { + deepEqual(obj1[prop], obj2[prop]); + } + else if(obj1[prop] != obj2[prop]) { + return false; + } + } + return true; } diff --git a/chapter05.js b/chapter05.js index d1372cb..4d31386 100644 --- a/chapter05.js +++ b/chapter05.js @@ -21,12 +21,25 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { // Your code here + return arrays.reduce(function(a, b) { + return a.concat(b); + }, []); } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { // Your code here + var count = sumAges = 0; + for(person in byName) { + ancestry.forEach(function(bio) { + if(person == bio.mother) { + count++; + sumAges += bio.born - byName[person].born; + } + }); + } + return sumAges / count; } // Problem 3: Historical life expectancy @@ -35,6 +48,20 @@ function averageMomChildAgeDiff() { */ function averageAgeByCentury() { // Your code here + function assignCentury(person) { + return Math.ceil(person.died / 100); + } + +// Each inner array represents a century + var avgAge = {16: [], 17: [], 18: [], 19: [], 20: [], 21: []}; + ancestry.forEach(function(person) { + avgAge[assignCentury(person)].push(person.died - person.born); + }); + + for(century in avgAge) { + avgAge[century] = average(avgAge[century]); + } + return avgAge; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..3b39a89 100644 --- a/chapter13.js +++ b/chapter13.js @@ -10,6 +10,29 @@ // Problem 1: Build table function buildTable(data) { // Your code here + var table = document.createElement('table'); + var headerRow = document.createElement('tr'); + table.appendChild(headerRow); + + var textNode, htmlNode, currRow, initial = 1; + data.forEach(function(element) { + currRow = document.createElement('tr'); + table.appendChild(currRow); + for(prop in element) { + if(initial) { + htmlNode = document.createElement('th'); + textNode = document.createTextNode(prop); + htmlNode.appendChild(textNode); + headerRow.appendChild(htmlNode); + } + htmlNode = document.createElement('td'); + textNode = document.createTextNode(element[prop]); + htmlNode.appendChild(textNode); + currRow.appendChild(htmlNode); + } + initial = 0; + }); + return table; }