diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..e69de29 diff --git a/chapter04.js b/chapter04.js index 12f6ef0..a3be62c 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,42 +9,109 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + if (step == 0) { + step = 1; + } + let arr = []; + if (step >=1) { + for(let i=start; i<=end; i+=step) + arr.push(i); + } + else { + for(let i=start; i>=end; i+=step) + arr.push(i); + } + return arr; } function sum(array) { - // Your code here + let sum = 0; + for(let i=0; i= 0; i--) { + reversed.push(array[i]); + } + return reversed; } function reverseArrayInPlace(array) { - // Your code here + let mid = array.length / 2; + for(let i=0; i<=mid; i++){ + let temp = array[i]; + let opp_index = array.length-1-i; + array[i] = array[opp_index]; + array[opp_index] = temp; + } + return array; } // Problem 3: A List function arrayToList(array) { - // Your code here + let list = null; + for(let i=array.length-1; i>=0; i--){ + list = prepend(array[i], list) + } + return list; } function listToArray(list) { - // Your code here + let arr = []; + for(let node=list; node; node=node.rest) { + arr.push(node.value); + } + return arr; } function nth(list, position) { - // Your code here + let count = 0; + for(let node=list; node; node=node.rest) { + if(count == position) + return node.value; + count ++; + } + return undefined; } function prepend(element, list) { - // Your code here + let new_list = { + 'value': element, + 'rest': list + } + return new_list; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + //Check if identical + if (obj1 === obj2) { + return true; + } + + //Check type and nulls + if (obj1 == null || obj2 == null || typeof(obj1) != "object" || typeof(obj2) != "object") { + return false; + } + + //Check length + if (Object.keys(obj1).length != Object.keys(obj2).length) { + return false; + } + //Deep comparison + for(item in obj1) { + if (obj2.hasOwnProperty(item)) { + if ( !(deepEqual(obj1[item], obj2[item]))) { + return false; + } + } + } + return true; } diff --git a/chapter05.js b/chapter05.js index d1372cb..5f61852 100644 --- a/chapter05.js +++ b/chapter05.js @@ -8,25 +8,32 @@ const ancestry = require('./ancestry'); function average(array) { - function plus(a, b) { return a + b; } - return array.reduce(plus) / array.length; + function plus(a, b) { return a + b; } + return array.reduce(plus) / array.length; } const byName = {}; -ancestry.forEach(function(person) { - byName[person.name] = person; +ancestry.forEach((person) => { + byName[person.name] = person; }); // Problem 1: Flattening -function flatten(arrays) { - // Your code here +function flatten(array) { + return array.reduce((flat_arr, curr_arr) => { + return flat_arr.concat(curr_arr); + }, []) } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { - // Your code here + let differences = ancestry.filter((person) => { + return byName[person.mother] != null; //filter those who only have mothers in data + }).map((person) => { + return person.born - byName[person.mother].born; + }); + return average(differences); } // Problem 3: Historical life expectancy @@ -34,7 +41,18 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + let centuryAges = {}; + ancestry.forEach((person) => { + let century = Math.ceil(person.died / 100); + if (!(centuryAges.hasOwnProperty(century))) { + centuryAges[century] = []; + } + centuryAges[century].push(person.died-person.born); + }); + for (century in centuryAges) { + centuryAges[century] = average(centuryAges[century]); + } + return centuryAges; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..95f104d 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,7 +9,28 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + let table = document.createElement("table"); + let attributes = Object.keys(data[0]); + let top_row = document.createElement("tr"); + for (attrib in attributes) { + let header_cell = document.createElement('th'); + header_cell.textContent = attrib; + top_row.appendChild(header_cell); + } + table.appendChild(top_row); + data.forEach((obj) => { //Each mountain object + let table_row = document.createElement("tr"); + attributes.forEach((attrib) => { //Name, Height, Country values + let row_cell = document.createElement("td"); + row_cell.textContent = obj[attrib]; + if (typeof obj[attrib] == "number") { + row_cell.style.textAlign == "right"; + } + table_row.appendChild(row_cell); + }); + table.appendChild(table_row); + }); + return table; }