diff --git a/chapter04.js b/chapter04.js index 12f6ef0..15c825e 100644 --- a/chapter04.js +++ b/chapter04.js @@ -9,42 +9,112 @@ // Problem 1: The sum of a range function range(start, end, step=1) { - // Your code here + let result = []; + if (!step) { + step = 1; + } + if (step < 0) { + for (i = start; i >= end; i += step) { + result.push(i); + } + } + else { + for (i = start; i <= end; i += step) { + result.push(i); + } + } + return result; } function sum(array) { - // Your code here + let total = 0; + array.forEach( (x) => { total += x; }); + return total; } // Problem 2: Reversing an Array function reverseArray(array) { - // Your code here + let result = []; + for (let i = array.length - 1; i >= 0; i--) { + result.push(array[i]); + } + return result; } function reverseArrayInPlace(array) { - // Your code here + let l = array.length - 1; + for (let i = 0; i < array.length / 2; i++) { + let temp = array[i]; + array[i] = array[l - i]; + array[l - i] = temp; + } + return array; } // Problem 3: A List function arrayToList(array) { - // Your code here + let result = null; + for (let i = array.length - 1; i >= 0; i--) { + result = prepend(array[i], result); + } + return result; } function listToArray(list) { - // Your code here + let result = []; + for (let cursor = list; cursor != null; cursor = cursor.rest) { + result.push(cursor.value); + } + return result; } function nth(list, position) { - // Your code here + if (position == 0) { + return list.value; + } + for (let cursor = list; cursor; cursor = cursor.rest) { + return nth(cursor.rest, position - 1); + } } function prepend(element, list) { - // Your code here + let result = { + value: element, + rest: list + }; + return result; } +// -=-=-=-=-= helper func +function isValid(element) { + return typeof element == "object" && element != null; +} +// -=-=-=-=-=- + // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + if (obj1 === obj2) { + return true; + } + else if (isValid(obj1) && isValid(obj2)) { + for (let prop in obj1) { + if (!obj2.hasOwnProperty(prop)) { + return false; + } + if (!deepEqual(obj2[prop], obj1[prop])) { + return false; + } + } + for (let prop in obj2) { + if (!obj1.hasOwnProperty(prop)) { + return false; + } + if (!deepEqual(obj1[prop], obj2[prop])) { + return false; + } + } + return true; + } } diff --git a/chapter05.js b/chapter05.js index d1372cb..28b30e2 100644 --- a/chapter05.js +++ b/chapter05.js @@ -17,16 +17,24 @@ ancestry.forEach(function(person) { byName[person.name] = person; }); - // Problem 1: Flattening function flatten(arrays) { - // Your code here + return arrays.reduce( (a, b) => {return a.concat(b); }, []); } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ + function averageMomChildAgeDiff() { - // Your code here + let ageDifferences = []; + let filteredAncestry = ancestry.filter( (person) => {return byName[person.mother] != null ? true : false;} ); + filteredAncestry.forEach( (person) => { + let birth = person.born; + let motherDOB = byName[person.mother].born; + let ageDiff = birth - motherDOB; + ageDifferences.push(ageDiff); + }); + return average(ageDifferences); } // Problem 3: Historical life expectancy @@ -34,10 +42,24 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + averageAges = {}; + ancestry.forEach((person) => { + let century = Math.ceil(person.died / 100); + let age = person.died - person.born; + if (!averageAges.hasOwnProperty(century)) { + averageAges[century] = [age]; + } else { + averageAges[century].push(age); + } + }); + Object.keys(averageAges).forEach( (century) => { averageAges[century] = average(averageAges[century]); } ) + return averageAges; } + + + // Do not modify below here. module.exports = { flatten, averageMomChildAgeDiff, averageAgeByCentury }; diff --git a/chapter13.js b/chapter13.js index ab99bc6..0a3937b 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,9 +9,45 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + let table = document.createElement("table"); + let headings = Object.keys(data[0]); + let headerRow = createRow(headings, true); + table.appendChild(headerRow); + data.forEach( (element) => { + let rowData = Object.keys(data[0]).map((key) => { + return element[key]; + }); + let row = createRow(rowData); + table.appendChild(row); + }); + return table } +function createRow(data, isHeader) { + let row = document.createElement("tr"); + data.forEach((element) => { + let col = createCol(element, isHeader); + row.appendChild(col); + }); + return row; +}; + +function createCol(data, isHeader) { + let col; + if (isHeader){ + col = document.createElement("th"); + } + else{ + col = document.createElement("td"); + } + + let content = document.createTextNode(data); + col.appendChild(content); + if (typeof data == "number"){ + col.style.textAlign = "right"; + } + return col; +} // Do not modify below here. module.exports = { buildTable }; diff --git a/package-lock.json b/package-lock.json index 5459d67..f414ca3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2,7 +2,6 @@ "name": "eloquentjs-problems", "version": "1.0.0", "lockfileVersion": 1, - "requires": true, "dependencies": { "abab": { "version": "1.0.3",