diff --git a/chapter04.js b/chapter04.js index 12f6ef0..7d70838 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,42 +9,88 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + array = []; + if (start < end) { + for (let i = start; i <= end; i+=step) { + array.push(i); + } + } + if (start > end) { + for (let i = start; i >= end; i+=step) { + array.push(i); + } + } + return array; } function sum(array) { - // Your code here + num = 0; + for (let i = 0; i < array.length; i++) { + num += array[i]; + } + return num; } // 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..5896689 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,13 +20,22 @@ 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 + // 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 @@ -34,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)); + } } 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