From 6bbf93207d209d4626572e4d9316742140b868d6 Mon Sep 17 00:00:00 2001 From: Perry Raskin Date: Tue, 22 Aug 2017 16:41:30 -0400 Subject: [PATCH 1/3] Ch04 Problem 1 --- chapter04.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..1f7a21d 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,11 +9,26 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + array = []; + if (start < end) { + for (i = start; i <= end; i+=step) { + array.push(i); + } + } + if (start > end) { + for (i = start; i >= end; i+=step) { + array.push(i); + } + } + return array; } function sum(array) { - // Your code here + num = 0; + for (i = 0; i < array.length; i++) { + num += array[i]; + } + return num; } // Problem 2: Reversing an Array From ce782420a4283c82409b2c2ed8bca34c0f92c0cd Mon Sep 17 00:00:00 2001 From: Perry Raskin Date: Wed, 30 Aug 2017 15:32:46 -0400 Subject: [PATCH 2/3] Chapter 4 & some of 5 --- chapter04.js | 51 +++++++++++++++++++++++++++++++++++++---------- chapter05.js | 6 ++++-- chapter05.test.js | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/chapter04.js b/chapter04.js index 1f7a21d..7d70838 100644 --- a/chapter04.js +++ b/chapter04.js @@ -11,12 +11,12 @@ function range(start, end, step=1) { array = []; if (start < end) { - for (i = start; i <= end; i+=step) { + for (let i = start; i <= end; i+=step) { array.push(i); } } if (start > end) { - for (i = start; i >= end; i+=step) { + for (let i = start; i >= end; i+=step) { array.push(i); } } @@ -25,7 +25,7 @@ function range(start, end, step=1) { function sum(array) { num = 0; - for (i = 0; i < array.length; i++) { + for (let i = 0; i < array.length; i++) { num += array[i]; } return num; @@ -33,33 +33,64 @@ function sum(array) { // Problem 2: Reversing an Array function reverseArray(array) { - // Your code here + newArray = []; + for (let i = array.length-1; i >= 0; i--) { + newArray.push(array[i]); + } + return newArray; } function reverseArrayInPlace(array) { - // Your code here + let len = array.length, temp = 0; + for (let i = 0; i < len/2; i++) { + temp = array[i]; + array[i] = array[(len-1)-i]; + array[(len-1)-i] = temp; + } + arrayValue = array; } // Problem 3: A List function arrayToList(array) { - // Your code here + let list = null; + for (let j = array.length - 1; j >= 0; j--) { + list = { value: array[j], rest: list, }; + } + return list; } function listToArray(list) { - // Your code here + let array = []; +  while(list) { +    array.push(list.value); +    list = list.rest; +  } +  return array; } function nth(list, position) { - // Your code here + let n = 0; + while (n < position && list) { + list = list.rest; + n++; + } + return list.value; } function prepend(element, list) { - // Your code here + const newList = { + value: element, + rest: list + }; + return newList; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + if (typeof obj1 !== typeof obj2) return false; + else if (obj1 == obj2) return true; + deepEqual(obj1.rest, obj2.rest); + return false; } diff --git a/chapter05.js b/chapter05.js index d1372cb..76fde56 100644 --- a/chapter05.js +++ b/chapter05.js @@ -1,4 +1,4 @@ -/* +x/* * Add your solutions to the chapter 5 problems from the eloquentjs book. * - DO NOT rename the functions below. * - You may add other functions if you need them. @@ -20,7 +20,9 @@ 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 diff --git a/chapter05.test.js b/chapter05.test.js index d65ea62..3f3af15 100644 --- a/chapter05.test.js +++ b/chapter05.test.js @@ -32,4 +32,4 @@ describe('Ch.5, Problem 3: Historical life expectancy', () => { expect(result[20]).toBeCloseTo(84.66, 1); expect(result[21]).toBeCloseTo(94); }); -}); +}); \ No newline at end of file From 260bb2559f97008919ef58c638638a6ba8fb2e5b Mon Sep 17 00:00:00 2001 From: Perry Raskin Date: Wed, 30 Aug 2017 23:13:17 -0400 Subject: [PATCH 3/3] ch4-5, working on 13 --- chapter05.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/chapter05.js b/chapter05.js index 76fde56..5896689 100644 --- a/chapter05.js +++ b/chapter05.js @@ -28,7 +28,14 @@ function flatten(arrays) { // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { - // Your code here + // Only get values for mother + var array = ancestry.filter(function(person) { + return byName[person.mother] != null; + // Get the birth date of the current array value + // Subtract that value from the birthdate of the mother from byName + }).map(function(person) { + return person.born - byName[person.mother].born; + }); } // Problem 3: Historical life expectancy @@ -36,7 +43,27 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + var centuries = {}; + // Map through values of an array 'a' + a.map(function (person) { + + // Century of death + var century = Math.ceil(person.died / 100); + // Age when died + var age = person.died - person.born; + // Check if century value doesn't exist yet + if (!(century in centuries)) { + // Insert the century, make it an array of the age + centuries[century] = [age]; + } else { + // Otherwise, add the current age + centuries[century].push(age); + } + }); + + for (var key in centuries) { + console.log(key + ': ' + average(centuries[key]).toFixed(3)); + } }