From 16e1eb91e1201bfa6588bf1c4ebc781b25427a0a Mon Sep 17 00:00:00 2001 From: Yvan Pangilinan Date: Wed, 30 Aug 2017 13:35:03 -0400 Subject: [PATCH] Excersises I finished. --- chapter04.js | 68 ++++++++++++++++++++++++++++++++++++++++++++-------- chapter05.js | 9 ++++++- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/chapter04.js b/chapter04.js index 12f6ef0..489cf4f 100644 --- a/chapter04.js +++ b/chapter04.js @@ -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 + } diff --git a/chapter05.js b/chapter05.js index d1372cb..884d481 100644 --- a/chapter05.js +++ b/chapter05.js @@ -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 @@ -40,4 +48,3 @@ function averageAgeByCentury() { // Do not modify below here. module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury }; -