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
68 changes: 58 additions & 10 deletions chapter04.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,91 @@


// Problem 1: The sum of a range
function range(start, end, step=1) {
// Your code here
function range(num1, num2, num3=1) {
// Idk why this code is crashing the test
// var arr =[];
//
// while(num1 !== num2 && num1 >= 0){
// arr.push(num1);
// num1 += num3;
// }
// arr.push(num2);
// return arr;
}

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

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

function reverseArrayInPlace(array) {
function reverseArrayInPlace(arr) {
// Your code here
var temp = 0;
var count = 0;
for(var i = arr.length-1; i >= arr.length/2; i--){
temp = arr[count];
arr[count] = arr[i];
arr[i] = temp;

count++;
}
return arr;
}

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

function listToArray(list) {
// Your code here
var arr = [];
while(list){
arr.push(list.value);
list = list.rest;
}
return arr;
}

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

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

// Problem 4: Deep comparison
function deepEqual(obj1, obj2) {
function deepEqual(comp1, comp2) {
// Your code here

}


Expand Down
9 changes: 8 additions & 1 deletion chapter05.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ ancestry.forEach(function(person) {
// Problem 1: Flattening
function flatten(arrays) {
// Your code here
var newArr = arrays.reduce(function(startArr, currArr){
return startArr.concat(currArr);
});
}

// Problem 2: Mother-child age difference
/* This must return the average age difference instead of printing it */
function averageMomChildAgeDiff() {
// Your code here
function mother(p) { return byName[p.mother] != null; };

var ageDiff = ancestry.filter(mother).map(function(p) {
return p.born - byName[p.mother].born;
});
}

// Problem 3: Historical life expectancy
Expand All @@ -40,4 +48,3 @@ function averageAgeByCentury() {

// Do not modify below here.
module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury };