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

if(step == 1){
for(var i = start; i <= end; i++){
range_arr.push(i);
}
} else {
if(start > end){
while (start >= end){
range_arr.push(start);
start = start + step;
}
} else {
while (start <= end){
range_arr.push(start);
start = start + step;
}
}
}
return range_arr;
}

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 arr = [];
var counter = Math.floor(array.length/2);
for(var i = array.length - 1; i >= 0; i--){
arr.push(array[i]);
}
return arr;
}

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

// Problem 3: A List
function arrayToList(array) {
// Your code here
var list = {
value: null,
rest: null
};
var i = 0;
for(var node = list; i < array.length; node = node.rest){
node.value = array[i];
if(i != array.length-1){
node.rest = new Object();
} else {
node.rest = null;
}
i++;
}
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) {
function nth(list, pos) {
// Your code here
var counter = 0;
for(var node = list; counter <= pos; node = node.rest){
if(counter == pos){
return node.value;
}
counter++;
}
return undefined;
}

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
// Your code here
if((typeof(obj1) == "object" && obj1 != null) && (typeof(obj2) == "object" && obj2 != null)){
if(typeof(obj1.here) == "object" && typeof(obj2.here) == "object"){
if(obj1.is === obj2.is && obj1.object === obj2.object){
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return obj1 === obj2;
}
}


// Do not modify below here.
module.exports = {
range, sum, reverseArray, reverseArrayInPlace,
arrayToList, listToArray, nth, prepend, deepEqual
};
};
50 changes: 50 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,38 @@ ancestry.forEach(function(person) {
// Problem 1: Flattening
function flatten(arrays) {
// Your code here
return reduce(arrays, function(a, b){
return a.concat(b);
}, 0);
}

function reduce(array, combine, start) {
var current = [];
for (var i = start; i < array.length; i++)
current = combine(current, array[i]);
return current;
}

// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
return average(map(hasKnownMother,
function(person){
return person.born - byName[person.mother].born;
}));
}

var hasKnownMother= ancestry.filter(function(person){
if (byName[person.mother]){ return person; }
});

function map(array, transform){
var diff = [];
for(var i= 0; i < array.length; i++){
diff.push(transform(array[i]));
}
return diff;
}

// Problem 3: Historical life expectancy
Expand All @@ -35,9 +61,33 @@ function averageMomChildAgeDiff() {
*/
function averageAgeByCentury() {
// Your code here
return setAverage(findCentury(ancestry));
}

function findCentury(array){
var listByCentury = {};
for(var i = 0; i < array.length; i++){
var century = getCentury(array[i]);
if(!listByCentury[century]){
listByCentury[century] = [];
}
listByCentury[century].push(array[i].died - array[i].born);
}
return listByCentury;
}

function getCentury(person){
return Math.floor(person.died/100 + 1);
}

function setAverage(cObject){
var keys = Object.keys(cObject);
var averageObj = {};
for(var i = 0; i < keys.length; i++){
averageObj[keys[i]] = Math.ceil(average(cObject[keys[i]])*100)/100;
}
return averageObj;
}
// Do not modify below here.
module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury };

29 changes: 29 additions & 0 deletions chapter13.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@
// Problem 1: Build table
function buildTable(data) {
// Your code here
var table = document.createElement("table");
var table_row = document.createElement("tr");
var thead_data = Object.keys(data[0]);

for(var i = 0; i < thead_data.length; i++){
var head_element = thead_data[i];
var table_head = document.createElement("th");
table_head.appendChild(document.createTextNode(head_element));
table_row.appendChild(table_head);
}
table.appendChild(table_row); //table header

for(var k = 0; k < data.length; k++){
table_row = document.createElement("tr");
//add all three
for(var m = 0; m < thead_data.length; m++){
var head_col_name = thead_data[m];
var table_data = document.createElement("td");
var current_obj = data[k];
table_data.appendChild(document.createTextNode(current_obj[head_col_name]));

if(typeof current_obj[head_col_name] == "number"){
table_data.style.textAlign = "right";
}
table_row.appendChild(table_data);
}
table.appendChild(table_row);
}
return table;
}


Expand Down