diff --git a/chapter04.js b/chapter04.js index 12f6ef0..eeaa304 100644 --- a/chapter04.js +++ b/chapter04.js @@ -6,50 +6,134 @@ * - DO NOT modify the number of parameters for each function. */ - // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here -} + var array = []; + if (arguments.length === 3) { //if 3rd argument is entered + step = arguments[2]; + } + if (start > end) { // decrement or reverse in descending order + step = -1; + } + var negative = function(i, end) { + if (start > end || step < 0) { + return i >= end; // largest number be the start + } else { + return i <= end; + } + } + for (var i = start; negative(i, end); i+=step) { + array.push(i); + } + return array; + +}; 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 newArray = []; + for (var i = 0; i < array.length; i++) { + newArray.unshift(array[i]); // insert each element at its start + } + return newArray; } function reverseArrayInPlace(array) { - // Your code here + var temp = []; + for (var i = 0; i < Math.floor(array.length/2);i++) { //round down and loop over half the length of the array + temp[i] = array[i]; //avoid overwritting + array[i] = array[array.length - 1 - i]; //replace first half with other back half + array[array.length - 1 - i] = temp[i]; //replace back half with temporary variable + } + return array; } + + + // Problem 3: A List -function arrayToList(array) { - // Your code here +function arrayToList (array) { + 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 +function listToArray (list) { + var holder = []; + for (var i = list; i; i = i.rest) { + holder.push(i.value); + } + return array; } -function nth(list, position) { - // Your code here +function nth (list, position) { + if list is null + undefined + else if position == 0 + list.value + else + nth(list.rest, position - 1) } -function prepend(element, list) { - // Your code here +function prepend (element, list) { + list = {value: element, rest: lists} + return list; } + + + // Problem 4: Deep comparison -function deepEqual(obj1, obj2) { - // Your code here -} +var deepEqual = function (obj1, obj2) { + if (obj1 === obj2) { + return true; + } + else if (obj1 == null || tpropertyInYeof obj1 != "object" || obj2 == null || tpropertyInYeof y != "object") + return false; + + var propertyInX = 0; + var propertyInY = 0; + + + for (var prop in obj1) { + propertyInX += 1; + } + + for (var prop in obj2) { + propertyInY += 1; + if (!(prop in obj1) || !deepEqual(obj1[prop], obj2[prop])) { + return false; + } + } + + return propertyInX === propertyInY; +}; + + + + // Do not modify below here. + module.exports = { + range, sum, reverseArray, reverseArrayInPlace, + arrayToList, listToArray, nth, prepend, deepEqual + }; diff --git a/chapter05.js b/chapter05.js index d1372cb..b7476f4 100644 --- a/chapter05.js +++ b/chapter05.js @@ -20,7 +20,13 @@ ancestry.forEach(function(person) { // Problem 1: Flattening function flatten(arrays) { - // Your code here + var arrays = [[1, 2, 3], [4, 5], [6]]; + console.log(arrays.reduce(function(prev, curr) { + + return prev.concat(curr); +}, )); + +// → [1, 2, 3, 4, 5, 6] } // Problem 2: Mother-child age difference @@ -33,11 +39,37 @@ function averageMomChildAgeDiff() { /* This must return the object/map with centuries as keys and average age for the century as the value */ -function averageAgeByCentury() { - // Your code here +function averageAgeByCentury(array) { + function plus(a, b) { return a + b; } + return array.reduce(plus) / array.length; } +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 + ": " + averageAgeByCentury(ages)); +} -// Do not modify below here. -module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury }; +} + + +// Do not modify below here. +module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury }; \ No newline at end of file