From 28186c3a894354face77721f2486b74b1403c274 Mon Sep 17 00:00:00 2001 From: Janiza Gesmundo Date: Fri, 1 Sep 2017 10:18:05 -0400 Subject: [PATCH 1/6] Add chapter04.js --- chapter04.js | 173 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 155 insertions(+), 18 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..b62d9b2 100644 --- a/chapter04.js +++ b/chapter04.js @@ -1,55 +1,192 @@ /* - * Add your solutions to the chapter 4 problems from the eloquentjs book. + * 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. * - You may rename the parameters. * - 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; +}; + +console.log(range(1, 10)); +// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +console.log(range(5, 2, -1)); +// → [5, 4, 3, 2] +console.log(sum(range(1, 10))); +// → 55 + + } + + + + + + + + + + // 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; } +console.log(reverseArray(["A", "B", "C"])); +// → ["C", "B", "A"]; +var arrayValue = [1, 2, 3, 4, 5]; +reverseArrayInPlace(arrayValue); +console.log(arrayValue); +// → [5, 4, 3, 2, 1] + + + + + + + // 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; } + +console.log(arrayToList([10, 20])); +// → {value: 10, rest: {value: 20, rest: null}} + +console.log(listToArray(arrayToList([10, 20, 30]))); +// → [10, 20, 30] + +console.log(prepend(10, prepend(20, null))); +// → {value: 10, rest: {value: 20, rest: null}} + +console.log(nth(arrayToList([10, 20, 30]), 1)); +// → 20 + + + + + + + + // 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; +}; + +var obj = {here: {is: "an"}, object: 2}; +console.log(deepEqual(obj, obj)); +// true +console.log(deepEqual(obj, {here: 1, object: 2})); +// false +console.log(deepEqual(obj, {here: {is: "an"}, object: 2})); +// true + + + // Do not modify below here. + module.exports = { + range, sum, reverseArray, reverseArrayInPlace, + arrayToList, listToArray, nth, prepend, deepEqual + }; From bd325c770adcea36db23913aeb7f979eff0e2b58 Mon Sep 17 00:00:00 2001 From: Janiza Gesmundo Date: Fri, 1 Sep 2017 10:18:17 -0400 Subject: [PATCH 2/6] Add chapter05.js --- chapter05.js | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) 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 From ce29a36e37ad3452bba0c34fb0e8fac48fc2746f Mon Sep 17 00:00:00 2001 From: Janiza Gesmundo Date: Fri, 1 Sep 2017 10:25:42 -0400 Subject: [PATCH 3/6] Modified chapter04.js --- chapter04.js | 24 +----------------------- 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/chapter04.js b/chapter04.js index b62d9b2..538a90f 100644 --- a/chapter04.js +++ b/chapter04.js @@ -1,5 +1,5 @@ /* - * Add your solutions to the chapter 5 problems from the eloquentjs book. + * Add your solutions to the chapter 4 problems from the eloquentjs book. * - DO NOT rename the functions below. * - You may add other functions if you need them. * - You may rename the parameters. @@ -48,16 +48,6 @@ console.log(sum(range(1, 10))); } - - - - - - - - - - // Problem 2: Reversing an Array function reverseArray(array) { var newArray = []; @@ -85,11 +75,6 @@ console.log(arrayValue); // → [5, 4, 3, 2, 1] - - - - - // Problem 3: A List function arrayToList (array) { @@ -122,7 +107,6 @@ function prepend (element, list) { return list; } - console.log(arrayToList([10, 20])); // → {value: 10, rest: {value: 20, rest: null}} @@ -136,12 +120,6 @@ console.log(nth(arrayToList([10, 20, 30]), 1)); // → 20 - - - - - - // Problem 4: Deep comparison var deepEqual = function (obj1, obj2) { From 761d1cc01c2902967b3ba551d5167814f9c934a2 Mon Sep 17 00:00:00 2001 From: Janiza Gesmundo Date: Fri, 1 Sep 2017 10:26:47 -0400 Subject: [PATCH 4/6] Modified chapter04.js --- chapter04.js | 1 - 1 file changed, 1 deletion(-) diff --git a/chapter04.js b/chapter04.js index 538a90f..b20fcff 100644 --- a/chapter04.js +++ b/chapter04.js @@ -7,7 +7,6 @@ */ // Problem 1: The sum of a range - function range(start, end, step=1) { var array = []; if (arguments.length === 3) { //if 3rd argument is entered From b9494f7a0d91d78cf0dcbc35faa4e9d667edbde5 Mon Sep 17 00:00:00 2001 From: Janiza Gesmundo Date: Fri, 1 Sep 2017 10:35:32 -0400 Subject: [PATCH 5/6] Modified chapter04.js --- chapter04.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chapter04.js b/chapter04.js index b20fcff..cfafab5 100644 --- a/chapter04.js +++ b/chapter04.js @@ -47,6 +47,8 @@ console.log(sum(range(1, 10))); } + + // Problem 2: Reversing an Array function reverseArray(array) { var newArray = []; @@ -74,6 +76,7 @@ console.log(arrayValue); // → [5, 4, 3, 2, 1] + // Problem 3: A List function arrayToList (array) { @@ -119,6 +122,7 @@ console.log(nth(arrayToList([10, 20, 30]), 1)); // → 20 + // Problem 4: Deep comparison var deepEqual = function (obj1, obj2) { From 3162e360565a358049412b824b631a691fffe42f Mon Sep 17 00:00:00 2001 From: Janiza G Date: Fri, 1 Sep 2017 10:39:22 -0400 Subject: [PATCH 6/6] Update chapter04.js --- chapter04.js | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/chapter04.js b/chapter04.js index cfafab5..eeaa304 100644 --- a/chapter04.js +++ b/chapter04.js @@ -36,19 +36,11 @@ function sum(array) { } return total; }; - -console.log(range(1, 10)); -// → [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -console.log(range(5, 2, -1)); -// → [5, 4, 3, 2] -console.log(sum(range(1, 10))); -// → 55 - - } + // Problem 2: Reversing an Array function reverseArray(array) { var newArray = []; @@ -68,17 +60,10 @@ function reverseArrayInPlace(array) { return array; } -console.log(reverseArray(["A", "B", "C"])); -// → ["C", "B", "A"]; -var arrayValue = [1, 2, 3, 4, 5]; -reverseArrayInPlace(arrayValue); -console.log(arrayValue); -// → [5, 4, 3, 2, 1] // Problem 3: A List - function arrayToList (array) { var list = null; for (var i = array.length - 1; i >= 0; i--) { @@ -109,22 +94,10 @@ function prepend (element, list) { return list; } -console.log(arrayToList([10, 20])); -// → {value: 10, rest: {value: 20, rest: null}} - -console.log(listToArray(arrayToList([10, 20, 30]))); -// → [10, 20, 30] - -console.log(prepend(10, prepend(20, null))); -// → {value: 10, rest: {value: 20, rest: null}} - -console.log(nth(arrayToList([10, 20, 30]), 1)); -// → 20 // Problem 4: Deep comparison - var deepEqual = function (obj1, obj2) { if (obj1 === obj2) { return true; @@ -149,14 +122,7 @@ var deepEqual = function (obj1, obj2) { return propertyInX === propertyInY; }; - -var obj = {here: {is: "an"}, object: 2}; -console.log(deepEqual(obj, obj)); -// true -console.log(deepEqual(obj, {here: 1, object: 2})); -// false -console.log(deepEqual(obj, {here: {is: "an"}, object: 2})); -// true +