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

// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
var array = [];
var start_cpy = start;

for(var i=0; i<=(end-start); i=i+step){
array[i] = start_cpy;
start_cpy = start_cpy+1;
}
return array;
}

function sum(array) {
// Your code here
var sum = 0;
var old_sum = 0;
for(var i=0; i<range_array.length; i++){ //is there another way to cycle through elements of an array in javascript?
sum = range_array[i] + old_sum;
old_sum = range_array[i];
}
return sum;
}

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

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

// Problem 3: A List

/*
List (as in linked list in C++)

Objects, as generic blobs of values, can be used to build all sorts of data
structures. A common data structure is the list (not to be confused with
the array). A list is a nested set of objects, with the first object holding
a reference to the second, the second to the third, and so on.

var list = {
value : 1 ,
rest : {
value : 2,
rest : {
value : 3,
rest : null
}
}
};
*/

function arrayToList(array) {
// Your code here
if(array.length === 0){
return null;
}

return{
value: array.shift();
rest: arrayToList(array);
}
}

function listToArray(list) {
// Your code here
var array;
var i = 0;
while(list.value != null){
array[i] = list.value;
i++;
list = list.rest;
}
return array;
}

function nth(list, position) {
// Your code here
if(list.value === number){
return true;
}
else if(list.value === null){
return false;
}
else{
return nth(number,list.rest);
}
}

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

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

else if((typeof obj1 === typeof obj2) && typeof obj1 === object){
var obj1_keys = Object.keys(obj1);
var obj2_keys = Object.keys(obj2);

//testing to see if they have the same keys
for(var i=0; i<obj1_keys.length; i++){
var key_1 = obj1_keys[i];
var key_2 = obj2_keys[i];
if(obj1_keys[i]!=obj2_keys[i]) return false;
if(obj1[key_1]!=obj2[key_2]) return false;
}
return true;
}
}


Expand Down
63 changes: 60 additions & 3 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* - DO NOT modify the number of parameters for each function.
*/
const ancestry = require('./ancestry');
var _ = require("lodash");



function average(array) {
function plus(a, b) { return a + b; }
Expand All @@ -20,21 +23,75 @@ ancestry.forEach(function(person) {

// Problem 1: Flattening
function flatten(arrays) {
// Your code here
var reduced = _.reduce(arrays,function(concated_sofar,row){
/*concated_sofar is the running total of concatenations of arrays and row represents each array in this array of arrays */
/* the 1st value of concatenated_sofar is the 1st element in arrays and row is each element of arrays*/
return concated_sofar.concat(row);
})
return reduced;
}



// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
var ancestry_array = JSON.parse(ancestry);

var age_differences = _.map(ancestry_array,function(entry){
for (var i = ancestry_array.length - 1; i >= 0; i--) {
if(ancestry_array[i].name === entry.mother){
return entry.born - ancestry_array[i].born
}
}
});

var null_filter = _.filter(age_differences,function(entry){
if(entry != null){
return true;
}
else{
return false;
}
});

var final_avg = average(null_filter);
return final_avg;
}

// 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 ancestry_array = JSON.parse(ancestry);

var deaths_centuries = _.map(ancestry_array,function(entry){
var new_obj = {}
new_obj["age_at_death"] = entry.died - entry.born;
new_obj["century"] = Math.ceil(entry.died/100); //rounds up using ceiling
return new_obj;
});

var avgs = [];
var output_object = {};
for(var i=16; i<22; i++){
var ith_filter = _.filter(deaths_centuries,function(entry){
if(entry["century"]===i){
return true;
}
});

var ith_map = _.map(ith_filter,function(entry){
return entry.age_at_death;
});

var avg = average(ith_map);
avgs.push(avg);
output_object[i]=avg;
}
return output_object;
}


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
var output = "<table>\n";
var properties = Object.keys(data[0]);
var n = properties.length;

output += "\t<tr>\n";
for(var i=0; i < n; i++){
output += "\t\t<th>" + properties[i] + "</th>\n";
}
output += "\n\t</tr>";

for(var i=0; i<data.length; i++){
output+="\n\t<tr>\n";
var object_properties = Object.keys(data[i]);
var properties_end = object_properties.length;
for(var j=0; j<properties_end; j++){
output += "\t\t<td>" + (data[i][object_properties[j]]).toString() + "</td>\n";
}
output+= "\n\t</tr>";
}
output+="\n</table>";

return output;
}


Expand Down