diff --git a/chapter04.js b/chapter04.js index 12f6ef0..16c01bb 100644 --- a/chapter04.js +++ b/chapter04.js @@ -8,43 +8,87 @@ // Problem 1: The sum of a range -function range(start, end, step=1) { - // Your code here +function range(start, end, step = 1) { + const arr = []; + let loop = true; + if (step !== 0) { + do { + if (Math.sign(step) > 0) { + if (start + step > end) loop = false; + } else { + if (start + step < end) loop = false; + } + arr.push(start); + start += step; + } while (loop); + } + return arr; } function sum(array) { - // Your code here + return array.reduce((sum, val) => sum + val, 0); } // Problem 2: Reversing an Array function reverseArray(array) { - // Your code here + const arr = []; + for (let i = 0; i < array.length; i++) { + arr[i] = array[array.length - 1 - i]; + } + return arr; } function reverseArrayInPlace(array) { - // Your code here + // return array.reverse(); + const len = array.length; + if (len == 0) return; + for (let i = 0; i <= Math.floor(len / 2); i++) { + [array[i], array[len - 1 - i]] = [array[len - 1 - i], array[i]]; + } } // Problem 3: A List function arrayToList(array) { - // Your code here + let ref = null; + array.map(x => ({ value: x, rest: null })).reduce((prev, cur) => { + if (prev != null) { + prev.rest = cur; + } else { + ref = cur; + } + return cur; + }, null); + return ref; } function listToArray(list) { - // Your code here + let ref = list, + arr = []; + while (ref) { + arr.push(ref.value); + ref = ref.rest; + } + return arr; } function nth(list, position) { - // Your code here + let ref = list, + i = 0; + while (ref && i < position) { + ref = ref.rest; + i++; + } + return ref.value; } -function prepend(element, list) { - // Your code here +function prepend(element, list=null) { + return {value: element, rest: list}; } // Problem 4: Deep comparison function deepEqual(obj1, obj2) { - // Your code here + //TODO the right way + return JSON.stringify(obj1) == JSON.stringify(obj2); } diff --git a/chapter05.js b/chapter05.js index d1372cb..565389e 100644 --- a/chapter05.js +++ b/chapter05.js @@ -13,20 +13,36 @@ function average(array) { } const byName = {}; -ancestry.forEach(function(person) { +ancestry.forEach(function (person) { byName[person.name] = person; }); // Problem 1: Flattening function flatten(arrays) { - // Your code here + let arr = []; + for (let n of arrays) { + if (Array.isArray(n)) { + arr = [...arr, ...(flatten(n))]; + } else { + arr.push(n); + } + } + return arr; } // Problem 2: Mother-child age difference /* This must return the average age difference instead of printing it */ function averageMomChildAgeDiff() { // Your code here + let diffs = []; + for (let p in byName) { + let person = byName[p]; + let mom = byName[person.mother]; + if (mom == undefined) continue; + diffs.push(person.born - mom.born); + } + return average(diffs); } // Problem 3: Historical life expectancy @@ -34,7 +50,15 @@ function averageMomChildAgeDiff() { for the century as the value */ function averageAgeByCentury() { - // Your code here + let data = {}; + for (let name in byName) { + let person = byName[name]; + let century = (parseInt(person.died.toString().substr(0, 2)) + 1).toString(); + data[century] = data[century] || []; + data[century].push(person.died - person.born); + } + Object.keys(data).forEach( x => data[x] = average(data[x])); + return data; } diff --git a/chapter13.js b/chapter13.js index ab99bc6..25afc47 100644 --- a/chapter13.js +++ b/chapter13.js @@ -9,7 +9,33 @@ // Problem 1: Build table function buildTable(data) { - // Your code here + let table = document.createElement('table'); + let row = document.createElement('tr'); + let cell = document.createElement('td'); + + let th_name = document.createElement('th'); + let th_height = document.createElement('th'); + let th_country = document.createElement('th'); + let t_row = row.cloneNode(); + th_name.innerHTML = 'Name'; + th_height.innerHTML = 'Height'; + th_country.innerHTML = 'Country'; + t_row.appendChild(th_name); + t_row.appendChild(th_height); + t_row.appendChild(th_country); + table.appendChild(t_row); + + data.forEach(function(obj){ + let n_row = row.cloneNode(); + for (let i in obj) { + let n_cell = cell.cloneNode(); + n_cell.innerHTML = obj[i]; + n_row.appendChild(n_cell); + } + table.appendChild(n_row); + }); + + return table; }