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
81 changes: 81 additions & 0 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,122 @@
// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
let arr = [];

if (step > 0) {
for (let i = start; i <= end; i += step) {
arr.push(i);
}
}

if (step < 0) {
for (let i = start; i >= end; i += step) {
arr.push(i);
}
}

return arr;

}

function sum(array) {
// Your code here
let sum = 0;
array.forEach((x) => { sum += x;});

return sum;
}

// Problem 2: Reversing an Array
function reverseArray(array) {
// Your code here
let arr = [];
const arrayLength = array.length;

for (let i = arrayLength - 1; i >= 0; i -= 1) {
arr.push(array[i]);
}

return arr;
}

function reverseArrayInPlace(array) {
// Your code here
const arrLength = array.length;
for (let s = 0, e = arrLength-1; s < e;) {
let a = array[s];
array[s] = array[e];
array[e] = a;

s += 1;
e -= 1;
}

return array;
}

// Problem 3: A List
function arrayToList(array) {
// Your code here
var v = array[0];
array.splice(0, 1);

if (array.length === 0) {
return {value: v, rest: null};
}

return {value: v, rest: arrayToList(array)}
}

function listToArray(list) {
// Your code here
var arr = [];
while (list !== null) {
arr.push(list.value)
list = list.rest;
}

return arr;
}

function nth(list, position) {
// Your code here
if (list === null) {
return null;
}
if (position === 0) {
return list.value;
}

list = list.rest;
position -= 1;
return nth(list, position);
}

function prepend(element, list) {
// Your code here
var head = {value: element, rest: list};

return head;
}

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
if ((typeof(obj1) === "object" && typeof(obj1) !== null) && (typeof(obj2) === "object" && typeof(obj2) !== null)) {
for (var x in obj1) {
if (obj2.hasOwnProperty(x)) {
if (!deepEqual(obj1[x], obj2[x])) {
return false;
}
} else {
return false;
}
}
} else if (obj1 !== obj2) {
return false;
}
return true;
}


Expand Down
30 changes: 30 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,26 @@ ancestry.forEach(function(person) {
// Problem 1: Flattening
function flatten(arrays) {
// Your code here
var newArray = arrays.reduce((x, y) => {return x.concat(y);});
return newArray;
}

// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
var ageDiffs = []
for (person in byName) {
var child = byName[person];
if (child.mother) {
var mother = byName[child.mother];
if (mother) {
ageDiffs.push(child.born - mother.born);
}
}
}

return average(ageDiffs);
}

// Problem 3: Historical life expectancy
Expand All @@ -35,6 +49,22 @@ function averageMomChildAgeDiff() {
*/
function averageAgeByCentury() {
// Your code here
var ageGroups = {};
for (person in byName) {
var century = Math.ceil(byName[person].died / 100);
var personAge = byName[person].died - byName[person].born
if (ageGroups[century]) {
ageGroups[century].push(personAge);
} else {
ageGroups[century] = [personAge];
}
}

for (group in ageGroups) {
ageGroups[group] = average(ageGroups[group]);
}

return ageGroups;
}


Expand Down
22 changes: 22 additions & 0 deletions chapter13.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
// Problem 1: Build table
function buildTable(data) {
// Your code here
var table = document.createElement('table');
var headerRow = document.createElement('tr');

for (header in data[0]) {
var th = document.createElement('th');
th.textContent = header;
headerRow.appendChild(th);
}
table.appendChild(headerRow);

data.forEach(d => {
var row = document.createElement('tr');

for (prop in d) {
var td = document.createElement('td');
td.textContent = d[prop];
row.appendChild(td);
}
table.appendChild(row);
});

return table;
}


Expand Down