Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 55 additions & 9 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down
37 changes: 33 additions & 4 deletions chapter05.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -20,21 +20,50 @@ 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
/* This must return the object/map with centuries as keys and average age
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));
}
}


Expand Down
2 changes: 1 addition & 1 deletion chapter05.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ describe('Ch.5, Problem 3: Historical life expectancy', () => {
expect(result[20]).toBeCloseTo(84.66, 1);
expect(result[21]).toBeCloseTo(94);
});
});
});