From adc4a5af843ba861ef66845ca6759ad40ca1fa37 Mon Sep 17 00:00:00 2001 From: Brian C Date: Mon, 28 Aug 2017 23:08:51 -0400 Subject: [PATCH 1/2] problems complete --- chapter04.js | 89 +++++++++++++++++++++++++++++++++++++++++++++++----- chapter05.js | 67 +++++++++++++++++++++++++++++++++++++-- chapter13.js | 24 +++++++++++++- 3 files changed, 168 insertions(+), 12 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..ab5d653 100644 --- a/chapter04.js +++ b/chapter04.js @@ -10,44 +10,117 @@ // Problem 1: The sum of a range function range(start, end, step=1) { // Your code here + var numbers = []; + if(step > 0){ //Numbers increasing + while(start <= end){ + numbers.push(start); + start += step; + } + + } + if(step < 0){ //Numbers decreasing + while(start >= end){ + numbers.push(start); + start += step; + } + } + return numbers; } function sum(array) { - // Your code here + var total = 0; + for(var x=0; x=0;--x){ + var thisList = { + value: array[x], + rest: ourList + }; + ourList = thisList; + } + return ourList; } function listToArray(list) { - // Your code here + var ourArray = []; + for(var node = list; node != null; node = node.rest){ + ourArray.push(node.value); + } + return ourArray; } + function nth(list, position) { - // Your code here + if(list == null) return undefined; + if(position == 0 ) return list.value; + else{ + return nth(list.rest, position-1); + } } 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 + if((typeof obj1 == "object" && obj1 != null)&& (typeof obj2 == "object" && obj2 != null)){ + var ob1count=0; //Property count of obj1 + var ob2count=0; //Property county of obj2 + for(var key in obj1){ + ++ob1count; + } + for(var key in obj2){ + ++ob2count; + } + + if(ob1count != ob2count) return false; + + for(var prop in obj1){ + if(!deepEqual(obj1[prop],obj2[prop])) { + return false; + } + } + return true; + } + else{ + return obj1 === obj2; + } } + // Do not modify below here. module.exports = { range, sum, reverseArray, reverseArrayInPlace, diff --git a/chapter05.js b/chapter05.js index d1372cb..21cdb04 100644 --- a/chapter05.js +++ b/chapter05.js @@ -20,13 +20,42 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { - // Your code here + var newArr = arrays.reduce(function(a,b){ + return a.concat(b); + }); + return newArr; } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { - // Your code here + function average(array) { + function plus(a, b) { return a + b; } + return array.reduce(plus) / array.length; +} + +var byName = {}; +ancestry.forEach(function(person) { + byName[person.name] = person; +}); + +// My code here. +var ageDifs = []; +for(var person in byName){ + var momName = byName[person].mother; + if(typeof byName[momName] === "undefined"){ //Mom DNE + ageDifs.push(null); + } + else{//Mom is defined, let's get the age difference + ageDifs.push(byName[person].born - byName[momName].born); + } +} + +var finalArray = ageDifs.filter(function(entry){ + return entry != null; +}); +return average(finalArray); + } // Problem 3: Historical life expectancy @@ -34,7 +63,39 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + + function average(array) { + function plus(a, b) { return a + b; } + return array.reduce(plus) / array.length; + } + + function group(genAvg,person){ + var gen = Math.ceil(person.died / 100); + if(typeof genAvg[gen] === "undefined"){ + genAvg[gen] = []; + genAvg[gen].push(person.died - person.born); + } + else{ + genAvg[gen].push(person.died - person.born); + } + } + + //my code + + var byName = {}; + ancestry.forEach(function(person) { + byName[person.name] = person; + }); + + var genCollection = {}; + for(key in byName){ + group(genCollection, byName[key]); + } + for(generation in genCollection){ + //console.log(genCollection); + genCollection[generation] = average(genCollection[generation]); + } + return genCollection; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..4296a7f 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,7 +9,29 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + //Build headings + var myTable = document.createElement("table"); + var headings = document.createElement("tr"); + for(var key in data[0]){ + var node = document.createElement("th"); + var textNode = document.createTextNode(key); + node.appendChild(textNode); + headings.appendChild(node); + } + myTable.appendChild(headings); + //Build the entries + for(var x=0; x Date: Mon, 28 Aug 2017 23:33:23 -0400 Subject: [PATCH 2/2] aligned numbers to the right --- chapter13.js | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/chapter13.js b/chapter13.js index 4296a7f..fec1f1d 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,30 +9,33 @@ // Problem 1: Build table function buildTable(data) { - //Build headings - var myTable = document.createElement("table"); - var headings = document.createElement("tr"); - for(var key in data[0]){ - var node = document.createElement("th"); - var textNode = document.createTextNode(key); - node.appendChild(textNode); - headings.appendChild(node); - } - myTable.appendChild(headings); - //Build the entries - for(var x=0; x