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
89 changes: 81 additions & 8 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,117 @@
// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
var numbers = [];
if(step > 0){ //Numbers increasing
while(start <= end){
numbers.push(start);
start += step;
}

}
if(step < 0){ //Numbers decreasing
while(start >= end){
numbers.push(start);
start += step;
}
}
return numbers;
}

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

// Problem 2: Reversing an Array
function reverseArray(array) {
// Your code here
var newArray = [];
for(var x=0; x < array.length; ++x){
newArray.unshift(array[x]);
}
return newArray;
}

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

// Problem 3: A List
function arrayToList(array) {
// Your code here
var ourList = null;
for(var x=array.length-1; x>=0;--x){
var thisList = {
value: array[x],
rest: ourList
};
ourList = thisList;
}
return ourList;
}

function listToArray(list) {
// Your code here
var ourArray = [];
for(var node = list; node != null; node = node.rest){
ourArray.push(node.value);
}
return ourArray;
}


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

function prepend(element, list) {
// Your code here
var newList = {
value: element,
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)){
var ob1count=0; //Property count of obj1
var ob2count=0; //Property county of obj2
for(var key in obj1){
++ob1count;
}
for(var key in obj2){
++ob2count;
}

if(ob1count != ob2count) return false;

for(var prop in obj1){
if(!deepEqual(obj1[prop],obj2[prop])) {
return false;
}
}
return true;
}
else{
return obj1 === obj2;
}
}



// Do not modify below here.
module.exports = {
range, sum, reverseArray, reverseArrayInPlace,
Expand Down
67 changes: 64 additions & 3 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,82 @@ ancestry.forEach(function(person) {

// Problem 1: Flattening
function flatten(arrays) {
// Your code here
var newArr = arrays.reduce(function(a,b){
return a.concat(b);
});
return newArr;
}

// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}

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

// My code here.
var ageDifs = [];
for(var person in byName){
var momName = byName[person].mother;
if(typeof byName[momName] === "undefined"){ //Mom DNE
ageDifs.push(null);
}
else{//Mom is defined, let's get the age difference
ageDifs.push(byName[person].born - byName[momName].born);
}
}

var finalArray = ageDifs.filter(function(entry){
return entry != null;
});
return average(finalArray);

}

// 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

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

function group(genAvg,person){
var gen = Math.ceil(person.died / 100);
if(typeof genAvg[gen] === "undefined"){
genAvg[gen] = [];
genAvg[gen].push(person.died - person.born);
}
else{
genAvg[gen].push(person.died - person.born);
}
}

//my code

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

var genCollection = {};
for(key in byName){
group(genCollection, byName[key]);
}
for(generation in genCollection){
//console.log(genCollection);
genCollection[generation] = average(genCollection[generation]);
}
return genCollection;
}


Expand Down
29 changes: 27 additions & 2 deletions chapter13.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,33 @@

// Problem 1: Build table
function buildTable(data) {
// Your code here
}
//Build headings
var myTable = document.createElement("table");
var headings = document.createElement("tr");
for(var key in data[0]){
var node = document.createElement("th");
var textNode = document.createTextNode(key);
node.appendChild(textNode);
headings.appendChild(node);
}
myTable.appendChild(headings);
//Build the entries
for(var x=0; x<data.length; ++x){
var entry = data[x];
var row = document.createElement("tr");
for(var key in entry){
var item = document.createElement("td");
var itemText = document.createTextNode(entry[key]);
item.append(itemText);
if(!isNaN(entry[key])){
item.style.textAlign = "right";
}
row.append(item);
}
myTable.append(row);
}
return myTable;
}


// Do not modify below here.
Expand Down