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
50 changes: 49 additions & 1 deletion chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,89 @@
// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
var array = [];
for (var i = start; step > 1 || step === undefined ? i <= end : i >= end; step ? i = i + step : i++)
array.push(i);
return array;
}

function sum(array) {
// Your code here
var result = 0;
for(var i=0; i<array.length; i++){
result+=array[i];
}
return result;
}

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 = array.length - 1; i >= 0; i--)
newArray.push(array[i]);
return newArray;
}

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

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
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
var array = [];
for (var node = list; node; node = node.rest)
array.push(node.value);
return array;
}

function nth(list, position) {
// Your code here
if (!list)
return undefined;
else if (position == 0)
return list.value;
else
return nth(list.rest, position - 1);
}

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
// Your code heres
}


Expand Down
29 changes: 29 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ 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
var differences = ancestry.filter(function(person) {
return byName[person.mother] != null;
}).map(function(person) {
return person.born - byName[person.mother].born;
});
}

// Problem 3: Historical life expectancy
Expand All @@ -35,6 +42,28 @@ function averageMomChildAgeDiff() {
*/
function averageAgeByCentury() {
// Your code here
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 + ": " + average(ages));
}
}


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 fields = Object.keys(data[0]);
var headRow = document.createElement("tr");
fields.forEach(function(field) {
var headCell = document.createElement("th");
headCell.textContent = field;
headRow.appendChild(headCell);
});
table.appendChild(headRow);
data.forEach(function(object) {
var row = document.createElement("tr");
fields.forEach(function(field) {
var cell = document.createElement("td");
cell.textContent = object[field];
if (typeof object[field] == "number")
cell.style.textAlign = "right";
row.appendChild(cell);
});
table.appendChild(row);
});
return table;
}


Expand Down