diff --git a/chapter04.js b/chapter04.js index 12f6ef0..9160b68 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,42 +9,106 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + var size; + if(start <= end){ + size = (end-start)/step; + }else{ + size = (start-end)/step; + } + if(size < 0){ + size *= -1; + } + var a = []; + var num = start; + for(var i = 0; i <= size; i++){ + a[i] = num; + if(isNaN(step)){ + num++ + }else{ + num += step; + } + } + return a; } function sum(array) { - // Your code here + var sum = 0; + for(var i = 0; i < array.length; i++){ + sum += array[i]; + } + return sum; } // Problem 2: Reversing an Array function reverseArray(array) { - // Your code here + var a_rev = []; + var end = array.length-1; + for(var i = 0; i < array.length; i++){ + a_rev[i] = array[end]; + end--; + } + return a_rev; } function reverseArrayInPlace(array) { - // Your code here + var end = array.length-1; + for(var i = 0; i < array.length/2; i++){ + var tmp = array[i]; + array[i] = array[end]; + array[end] = tmp; + end--; + } + return array; } // Problem 3: A List function arrayToList(array) { - // Your code here + var list = null; + for(var i = array.length-1; i >= 0; i--){ + list = { + value: array[i], + rest: list + } + } + return list; } function listToArray(list) { - // Your code here + var a = []; + for(var i = 0; isNaN(list); i++){ + a[i] = list.value; + list = list.rest; + } + return a; } function nth(list, position) { - // Your code here + if(position == 0){ + return list.value; + } + return nth(list.rest, position-1); } function prepend(element, list) { - // Your code here + list = { + value: element, + rest: list + } + return list; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + if(typeof(obj1) != "object" || typeof(obj2) != "object"){ + return obj1 === obj2; + }else{ + for(var prop1 in obj1){ + for(var prop2 in obj2){ + return deepEqual(obj1[prop1], obj2[prop2]); + } + } + } + return obj1 === obj2; } diff --git a/chapter05.js b/chapter05.js index d1372cb..6e1e8eb 100644 --- a/chapter05.js +++ b/chapter05.js @@ -19,14 +19,40 @@ ancestry.forEach(function(person) { // Problem 1: Flattening +function concat(a, b){ + var a_end = a.length; + for(var i = 0; i < b.length; i++){ + a[a_end] = b[i]; + a_end++; + } + return a; +} + function flatten(arrays) { - // Your code here + var b = arrays[0]; + for(var i = 1; i < arrays.length; i++){ + b = concat(b, arrays[i]); + } + return b; } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { - // Your code here + var tot_age_diff = 0; + var sample_size = 0; + ancestry.forEach(function(child){ + for(var mom in byName){ + if(child.mother === byName[mom].name){ + var mom_age = child.born - byName[mom].born; + tot_age_diff += mom_age; + sample_size++; + break; + } + } + }); + var avg_age_diff = tot_age_diff/sample_size; + return avg_age_diff; } // Problem 3: Historical life expectancy @@ -34,7 +60,26 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + var centuries = {}; + ancestry.forEach(function(person){ + var century = Math.ceil(person.died / 100); + var found = false; + for(var c in centuries){ + if(c == century){ + centuries[century].push(person.died-person.born); + found = true; + break; + } + } + if(!found){ + centuries[century] = []; + centuries[century].push(person.died-person.born); + } + }); + for(c in centuries){ + centuries[c] = average(centuries[c]); + } + return centuries; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..bce99ea 100644 --- a/chapter13.js +++ b/chapter13.js @@ -10,6 +10,29 @@ // Problem 1: Build table function buildTable(data) { // Your code here + var table = document.createElement("table"); + var headrow = document.createElement("tr"); + + for(var k of Object.keys(data[0])){ + var headcell = document.createElement("th"); + var cell = document.createTextNode(k); + headcell.appendChild(cell); + headrow.appendChild(headcell); + table.appendChild(headrow); + } + + for(var mount of data){ + var bodyrow = document.createElement("tr"); + + for(var m of Object.values(mount)){ + var bodycell = document.createElement("td"); + var cell = document.createTextNode(m); + bodycell.appendChild(cell); + bodyrow.appendChild(bodycell); + } + table.appendChild(bodyrow); + } + return table; }