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
46 changes: 45 additions & 1 deletion chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,86 @@

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

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

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

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

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

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

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

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

if (obj1 == null || typeof a != "object" ||
obj2 == null || typeof b != "object")
return false;
}


Expand Down
29 changes: 29 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,37 @@ ancestry.forEach(function(person) {
// Problem 1: Flattening
function flatten(arrays) {
// Your code here
if(!Array.isArray(arrays)){
return [arrays];
}
var my_array = [];
for (var i = 0; i<arrays.length;i++){
my_array =my_array.concat(flatten(arrays[i]));
}
return my_array;
}

// 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;
});

var differences = ancestry.filter(function(person) {
return byName[person.mother] != null;
}).map(function(person) {
return person.born - byName[person.mother].born;
});

return average(differences);
}

// Problem 3: Historical life expectancy
Expand All @@ -35,6 +60,10 @@ function averageMomChildAgeDiff() {
*/
function averageAgeByCentury() {
// Your code here
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}
// still thinking
}


Expand Down