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

for(var i = start; i <= end; i+=step) {
numRange.push(i);
}
}
else {
for(var j = start; j >= end; j+=step){
numRange.push(j);
}
}
return numRange;
}

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 revArr = [];
for(var i = array.length - 1; i > -1; --i) {
revArr.push(array[i]);
}
return revArr;
}

function reverseArrayInPlace(array) {
// Your code here
var temp = 0;
for(var i = 0; i < array.length / 2; ++i) {
temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
}

// Problem 3: A List
function arrayToList(array) {
// Your code here
var list = {};
var node = list;
if(array.length > 0) {
list = {
value: array[0],
rest: {}
};
node = list;
}

for(var i = 1; i < array.length; ++i) {
if(i != array.length - 1){
node.rest = {
value: array[i],
rest: {}
};
}
else {
node.rest = {
value: array[i],
rest: null
};
}
node = node.rest;
}

return list;
}

function listToArray(list) {
// Your code here
var array = [];

var ptr = list;
while(ptr != null) {
array.push(ptr.value);
ptr = ptr.rest;
}
return array;
}

function nth(list, position) {
// Your code here
var ptr = list;
for(var i = 0; i < position; ++i) {
if(ptr.rest === null) {
return undefined;
}
else {
ptr = ptr.rest;
}
}
return ptr.value;
}

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

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


Expand Down
27 changes: 27 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ ancestry.forEach(function(person) {
// Problem 1: Flattening
function flatten(arrays) {
// Your code here
return arrays.reduce(function(a, b) {
return a.concat(b);
}, []);
}

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

// Problem 3: Historical life expectancy
Expand All @@ -35,6 +48,20 @@ function averageMomChildAgeDiff() {
*/
function averageAgeByCentury() {
// Your code here
function assignCentury(person) {
return Math.ceil(person.died / 100);
}

// Each inner array represents a century
var avgAge = {16: [], 17: [], 18: [], 19: [], 20: [], 21: []};
ancestry.forEach(function(person) {
avgAge[assignCentury(person)].push(person.died - person.born);
});

for(century in avgAge) {
avgAge[century] = average(avgAge[century]);
}
return avgAge;
}


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

var textNode, htmlNode, currRow, initial = 1;
data.forEach(function(element) {
currRow = document.createElement('tr');
table.appendChild(currRow);
for(prop in element) {
if(initial) {
htmlNode = document.createElement('th');
textNode = document.createTextNode(prop);
htmlNode.appendChild(textNode);
headerRow.appendChild(htmlNode);
}
htmlNode = document.createElement('td');
textNode = document.createTextNode(element[prop]);
htmlNode.appendChild(textNode);
currRow.appendChild(htmlNode);
}
initial = 0;
});
return table;
}


Expand Down