Last active
September 15, 2015 09:22
-
-
Save philipmadeley/302dfb96d7eb322e5295 to your computer and use it in GitHub Desktop.
EJ Exercises with explanations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 4.4 deep linking | |
| */ | |
| function deepEqual(a, b) { | |
| if (a === b) return true; | |
| if (a == null || typeof a != "object" || | |
| b == null || typeof b != "object") | |
| return false; | |
| var propsInA = 0, propsInB = 0; | |
| for (var prop in a) | |
| propsInA += 1; | |
| for (var prop in b) { | |
| propsInB += 1; | |
| if (!(prop in a) || !deepEqual(a[prop], b[prop])) | |
| return false; | |
| } | |
| return propsInA == propsInB; | |
| } | |
| var obj = {here: {is: "an"}, object: 2}; | |
| console.log(deepEqual(obj, obj)); | |
| // → true | |
| console.log(deepEqual(obj, {here: 1, object: 2})); | |
| // → false | |
| console.log(deepEqual(obj, {here: {is: "an"}, object: 2})); | |
| // → true | |
| /* 4.3 a list | |
| function arrayToList takes arg array. Make var list null. | |
| Loop over the arrays length, be sure to minus the counter! | |
| List should be an object, with value set to array index, rest is recursive executing list. | |
| Return list. | |
| function listToArray takes arg list. Var array empty. | |
| Loop over var node, and node equals list, counter is node.rest. | |
| Push the node.value, return the array. | |
| function prepend takes value and list args. Return the object value and rest list. | |
| function nth takes args list, n. | |
| Check if !list return undefined. Else if n == 0, return list.value. | |
| Else return recursive nth function list.rest and n -1. | |
| */ | |
| function arrayToList(array) { | |
| var list = null; | |
| for (var i = array.length - 1; i >= 0; i--) | |
| list = {value: array[i], rest: list}; | |
| return list; | |
| } | |
| function listToArray(list) { | |
| var array = []; | |
| for (var node = list; node; node = node.rest) | |
| array.push(node.value); | |
| return array; | |
| } | |
| function prepend(value, list) { | |
| return {value: value, rest: list}; | |
| } | |
| function nth(list, n) { | |
| if (!list) | |
| return undefined; | |
| else if (n == 0) | |
| return list.value; | |
| else | |
| return nth(list.rest, n - 1); | |
| } | |
| console.log(arrayToList([10, 20])); | |
| // → {value: 10, rest: {value: 20, rest: null}} | |
| console.log(listToArray(arrayToList([10, 20, 30]))); | |
| // → [10, 20, 30] | |
| console.log(prepend(10, prepend(20, null))); | |
| // → {value: 10, rest: {value: 20, rest: null}} | |
| console.log(nth(arrayToList([10, 20, 30]), 1)); | |
| // → 20 | |
| /* 4.2 reversing an array | |
| New array Method: create function reverseArray with one arg. | |
| create new var output to hold the new array. | |
| Start the Loop var i equal to array.length -1, decrease i NOT increase. (you start from the last array num). | |
| push to output the array[i]. Return ouptut. | |
| Same array Method: | |
| In the loop find the middle integer using math.floor and divide array length by 2. | |
| Make new var point to current array[i]. | |
| Make the array[i] point to the array[array.length] minus 1, -i. | |
| Point old to the array[array.length -1, -i] | |
| */ | |
| function reverseArray(array) { | |
| var output = []; | |
| for (var i = array.length - 1; i >= 0; i--) | |
| output.push(array[i]); | |
| return output; | |
| } | |
| function reverseArrayInPlace(array) { | |
| for (var i = 0; i < Math.floor(array.length / 2); i++) { | |
| var old = array[i]; | |
| array[i] = array[array.length - 1 - i]; | |
| array[array.length - 1 - i] = old; | |
| } | |
| return array; | |
| } | |
| console.log(reverseArray(["A", "B", "C"])); | |
| // → ["C", "B", "A"]; | |
| var arrayValue = [1, 2, 3, 4, 5]; | |
| reverseArrayInPlace(arrayValue); | |
| console.log(arrayValue); | |
| // → [5, 4, 3, 2, 1] | |
| // 4.1 sum of a range | |
| /*____________________________ | |
| Create function with 3 args, start, end, step. If the step is null make the step 1. | |
| Create array[]. If the step > 0 then loop the var i and increase i += step. | |
| Push (i) to the array. Repeat for Else. Return the array. | |
| Function sum requires 1 arg - array. Add a total counter set to 0. | |
| Loop over array.length and add the total of the array[i]. return total. | |
| */ | |
| function range(start, end, step) { | |
| if (step == null) step = 1; | |
| var array = []; | |
| if (step > 0) { | |
| for (var i = start; i <= end; i += step) | |
| array.push(i); | |
| } else { | |
| for (var i = start; i >= end; i += step) | |
| array.push(i); | |
| } | |
| return array; | |
| } | |
| function sum(array) { | |
| var total = 0; | |
| for (var i = 0; i < array.length; i++) | |
| total += array[i]; | |
| return total; | |
| } | |
| console.log(sum(range(1, 10))); | |
| // → 55 | |
| console.log(range(5, 2, -1)); | |
| // → [5, 4, 3, 2] | |
| // 3.3 Bean counting | |
| /*____________________________ | |
| Create function with 2 args, then counter. | |
| For loop the string.length and if the string.chartAt = the arg, increase the counter | |
| With countBs, return the other function and assign B as the arg. | |
| */ | |
| function countChar(string, ch) { | |
| var counted = 0; | |
| for (var i = 0; i < string.length; i++) | |
| if (string.charAt(i) == ch) | |
| counted += 1; | |
| return counted; | |
| } | |
| function countBs(string) { | |
| return countChar(string, "B"); | |
| } | |
| // 3.2 Recursion | |
| /*____________________________ | |
| Create four conditional if statements. | |
| 2 Conditionals check if the remainder is 0 (true) or 1 (false) | |
| 2 Conditionals are recursive and execute the function loop again until n = 0 or 1 | |
| */ | |
| function isEven(n) { | |
| if (n == 0) | |
| return true; | |
| else if (n == 1) | |
| return false; | |
| else if (n < 0) | |
| return isEven(-n); | |
| else | |
| return isEven(n - 2); | |
| } | |
| // 3.1 return smallest number | |
| /* ____________________________ | |
| return Math.min method on args */ | |
| function min(a, b) { | |
| return Math.min(a, b); | |
| } | |
| // 2.3 Chess board | |
| /*____________________________ | |
| Create vars Size and blank string Board. | |
| Make two for loops contained within each other, loop X contains loop Y. | |
| If x and y leaves % 2 == 0, then increase with blank or else add # | |
| After y loop exits, add line break for board then console board. | |
| */ | |
| var size = 8; | |
| var board = ""; | |
| for (var x = 0; x < size; x++) { | |
| for (var y = 0; y < size; y++) { | |
| if((x + y) % 2 == 0) | |
| board += " "; | |
| else | |
| board += "#"; | |
| } | |
| board += "\n"; | |
| } | |
| console.log(board); | |
| // 2.2 FizzBuzz loop with exceptions. | |
| /*____________________________ | |
| for loop less than 100, with a counter increase of 1 | |
| Create empty var string, then create two if conditions to check 3 and 5 % = 0. Do not use else if becuase it will overide FizzBuzz. | |
| console the output or the number | |
| */ | |
| for (var n = 0; n < 100; n++) { | |
| var output = ""; | |
| if (n % 3 == 0) | |
| output += "Fizz" | |
| if (n % 5 == 0) | |
| output += "Buzz" | |
| console.log(output || n); | |
| } | |
| // 2.1 Loop a triangle | |
| /*____________________________ | |
| create var, line length less than 8, then increase by one | |
| */ | |
| for (var line = "#"; line.length < 8; line += "#") | |
| console.log(line); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment