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
66 changes: 55 additions & 11 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,87 @@


// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
function range(start, end, step = 1) {
const arr = [];
let loop = true;
if (step !== 0) {
do {
if (Math.sign(step) > 0) {
if (start + step > end) loop = false;
} else {
if (start + step < end) loop = false;
}
arr.push(start);
start += step;
} while (loop);
}
return arr;
}

function sum(array) {
// Your code here
return array.reduce((sum, val) => sum + val, 0);
}

// Problem 2: Reversing an Array
function reverseArray(array) {
// Your code here
const arr = [];
for (let i = 0; i < array.length; i++) {
arr[i] = array[array.length - 1 - i];
}
return arr;
}

function reverseArrayInPlace(array) {
// Your code here
// return array.reverse();
const len = array.length;
if (len == 0) return;
for (let i = 0; i <= Math.floor(len / 2); i++) {
[array[i], array[len - 1 - i]] = [array[len - 1 - i], array[i]];
}
}

// Problem 3: A List
function arrayToList(array) {
// Your code here
let ref = null;
array.map(x => ({ value: x, rest: null })).reduce((prev, cur) => {
if (prev != null) {
prev.rest = cur;
} else {
ref = cur;
}
return cur;
}, null);
return ref;
}

function listToArray(list) {
// Your code here
let ref = list,
arr = [];
while (ref) {
arr.push(ref.value);
ref = ref.rest;
}
return arr;
}

function nth(list, position) {
// Your code here
let ref = list,
i = 0;
while (ref && i < position) {
ref = ref.rest;
i++;
}
return ref.value;
}

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
//TODO the right way
return JSON.stringify(obj1) == JSON.stringify(obj2);
}


Expand Down
30 changes: 27 additions & 3 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,52 @@ function average(array) {
}

const byName = {};
ancestry.forEach(function(person) {
ancestry.forEach(function (person) {
byName[person.name] = person;
});


// Problem 1: Flattening
function flatten(arrays) {
// Your code here
let arr = [];
for (let n of arrays) {
if (Array.isArray(n)) {
arr = [...arr, ...(flatten(n))];
} else {
arr.push(n);
}
}
return arr;
}

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

// 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
let data = {};
for (let name in byName) {
let person = byName[name];
let century = (parseInt(person.died.toString().substr(0, 2)) + 1).toString();
data[century] = data[century] || [];
data[century].push(person.died - person.born);
}
Object.keys(data).forEach( x => data[x] = average(data[x]));
return data;
}


Expand Down
28 changes: 27 additions & 1 deletion chapter13.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,33 @@

// Problem 1: Build table
function buildTable(data) {
// Your code here
let table = document.createElement('table');
let row = document.createElement('tr');
let cell = document.createElement('td');

let th_name = document.createElement('th');
let th_height = document.createElement('th');
let th_country = document.createElement('th');
let t_row = row.cloneNode();
th_name.innerHTML = 'Name';
th_height.innerHTML = 'Height';
th_country.innerHTML = 'Country';
t_row.appendChild(th_name);
t_row.appendChild(th_height);
t_row.appendChild(th_country);
table.appendChild(t_row);

data.forEach(function(obj){
let n_row = row.cloneNode();
for (let i in obj) {
let n_cell = cell.cloneNode();
n_cell.innerHTML = obj[i];
n_row.appendChild(n_cell);
}
table.appendChild(n_row);
});

return table;
}


Expand Down