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
Empty file added .eslintrc
Empty file.
85 changes: 76 additions & 9 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,109 @@

// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
if (step == 0) {
step = 1;
}
let arr = [];
if (step >=1) {
for(let i=start; i<=end; i+=step)
arr.push(i);
}
else {
for(let i=start; i>=end; i+=step)
arr.push(i);
}
return arr;
}

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

// Problem 2: Reversing an Array
function reverseArray(array) {
// Your code here
let reversed = [];
let last = array.length - 1
for(let i = last; i >= 0; i--) {
reversed.push(array[i]);
}
return reversed;
}

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

// Problem 3: A List
function arrayToList(array) {
// Your code here
let list = null;
for(let i=array.length-1; i>=0; i--){
list = prepend(array[i], list)
}
return list;
}

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

function nth(list, position) {
// Your code here
let count = 0;
for(let node=list; node; node=node.rest) {
if(count == position)
return node.value;
count ++;
}
return undefined;
}

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
//Check if identical
if (obj1 === obj2) {
return true;
}

//Check type and nulls
if (obj1 == null || obj2 == null || typeof(obj1) != "object" || typeof(obj2) != "object") {
return false;
}

//Check length
if (Object.keys(obj1).length != Object.keys(obj2).length) {
return false;
}
//Deep comparison
for(item in obj1) {
if (obj2.hasOwnProperty(item)) {
if ( !(deepEqual(obj1[item], obj2[item]))) {
return false;
}
}
}
return true;
}


Expand Down
34 changes: 26 additions & 8 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,51 @@
const ancestry = require('./ancestry');

function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}

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


// Problem 1: Flattening
function flatten(arrays) {
// Your code here
function flatten(array) {
return array.reduce((flat_arr, curr_arr) => {
return flat_arr.concat(curr_arr);
}, [])
}

// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
let differences = ancestry.filter((person) => {
return byName[person.mother] != null; //filter those who only have mothers in data
}).map((person) => {
return person.born - byName[person.mother].born;
});
return average(differences);
}

// 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 centuryAges = {};
ancestry.forEach((person) => {
let century = Math.ceil(person.died / 100);
if (!(centuryAges.hasOwnProperty(century))) {
centuryAges[century] = [];
}
centuryAges[century].push(person.died-person.born);
});
for (century in centuryAges) {
centuryAges[century] = average(centuryAges[century]);
}
return centuryAges;
}


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

// Problem 1: Build table
function buildTable(data) {
// Your code here
let table = document.createElement("table");
let attributes = Object.keys(data[0]);
let top_row = document.createElement("tr");
for (attrib in attributes) {
let header_cell = document.createElement('th');
header_cell.textContent = attrib;
top_row.appendChild(header_cell);
}
table.appendChild(top_row);
data.forEach((obj) => { //Each mountain object
let table_row = document.createElement("tr");
attributes.forEach((attrib) => { //Name, Height, Country values
let row_cell = document.createElement("td");
row_cell.textContent = obj[attrib];
if (typeof obj[attrib] == "number") {
row_cell.style.textAlign == "right";
}
table_row.appendChild(row_cell);
});
table.appendChild(table_row);
});
return table;
}


Expand Down