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
82 changes: 73 additions & 9 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,106 @@

// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
var size;
if(start <= end){
size = (end-start)/step;
}else{
size = (start-end)/step;
}
if(size < 0){
size *= -1;
}
var a = [];
var num = start;
for(var i = 0; i <= size; i++){
a[i] = num;
if(isNaN(step)){
num++
}else{
num += step;
}
}
return a;
}

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

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

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

// 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 a = [];
for(var i = 0; isNaN(list); i++){
a[i] = list.value;
list = list.rest;
}
return a;
}

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

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
if(typeof(obj1) != "object" || typeof(obj2) != "object"){
return obj1 === obj2;
}else{
for(var prop1 in obj1){
for(var prop2 in obj2){
return deepEqual(obj1[prop1], obj2[prop2]);
}
}
}
return obj1 === obj2;
}


Expand Down
51 changes: 48 additions & 3 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,67 @@ ancestry.forEach(function(person) {


// Problem 1: Flattening
function concat(a, b){
var a_end = a.length;
for(var i = 0; i < b.length; i++){
a[a_end] = b[i];
a_end++;
}
return a;
}

function flatten(arrays) {
// Your code here
var b = arrays[0];
for(var i = 1; i < arrays.length; i++){
b = concat(b, arrays[i]);
}
return b;
}

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

// 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
var centuries = {};
ancestry.forEach(function(person){
var century = Math.ceil(person.died / 100);
var found = false;
for(var c in centuries){
if(c == century){
centuries[century].push(person.died-person.born);
found = true;
break;
}
}
if(!found){
centuries[century] = [];
centuries[century].push(person.died-person.born);
}
});
for(c in centuries){
centuries[c] = average(centuries[c]);
}
return centuries;
}


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 headrow = document.createElement("tr");

for(var k of Object.keys(data[0])){
var headcell = document.createElement("th");
var cell = document.createTextNode(k);
headcell.appendChild(cell);
headrow.appendChild(headcell);
table.appendChild(headrow);
}

for(var mount of data){
var bodyrow = document.createElement("tr");

for(var m of Object.values(mount)){
var bodycell = document.createElement("td");
var cell = document.createTextNode(m);
bodycell.appendChild(cell);
bodyrow.appendChild(bodycell);
}
table.appendChild(bodyrow);
}
return table;
}


Expand Down