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

// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
var arr = [];
if(step < 0){
for(i=start;i>=end;i+=step) {
arr.push(i)
}
} else {
for(i=start;i<=end;i+=step){
arr.push(i);
}
}
return arr;
}

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

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

}

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

// Problem 3: A List
function arrayToList(array) {
// Your code here
}
function arrayToList(arr, fob){
if(arr.length == 0)
return fob;
//test wants null rather than undefined
if (typeof fob == 'undefined'){
var list = {
value: arr.pop(),
rest: null
}}
else
var list = {
value: arr.pop(),
rest: fob
}
return arrayToList(arr,list);}

function listToArray(list) {
// Your code here
function listToArray(fob, arr){
if(typeof fob == 'undefined' | fob == null){
return arr;
}
if(typeof arr == 'undefined'){
arr = [];
}
arr.push(fob.value);
return listToArray(fob.rest,arr);
}

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

function prepend(element, list) {
// Your code here
function prepend(x, fob){
var fab = {value: x, rest: fob};
console.log(fab)
return fab
}

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

if(thing1 != null && thing2 != null) {
for(var name in thing1){
console.log("Comparing " + name)
if(!deepEqual(thing1[name],thing2[name])) {
return false }
}
}
return true

}


Expand Down
11 changes: 11 additions & 0 deletions chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ function flatten(arrays) {
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
function difference(person) {
if(byName[person.mother]) {
return person.born - byName[person.mother].born }
else return null }

function notNull(age) {
if (age != null)
return age
}

return average(ancestry.map(difference).filter(notNull)))
}

// Problem 3: Historical life expectancy
Expand Down