From 22b204376180b2d1cc52918cefce859050b21626 Mon Sep 17 00:00:00 2001 From: "jonah.m.delaney@gmail.com" Date: Thu, 20 Sep 2018 16:02:04 -0500 Subject: [PATCH 1/7] finished factorial --- src/recursion.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 12ca0a3d7..194e77fb9 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -4,32 +4,46 @@ // denoted by n!, is the product of all positive integers less than or equal to n. // Example: 5! = 5 x 4 x 3 x 2 x 1 = 120 // factorial(5); // 120 -var factorial = function(n) { +var factorial = function(n, sum=1, i=n) { + if (n < 0) {return null;} + if (i === 0) {return sum;} + sum *= i; + i--; + return factorial(n - 1, sum); }; // 2. Compute the sum of an array of integers. // Example: sum([1, 2, 3, 4, 5, 6]); // 21 -var sum = function(array) { +var sum = function(array, total=0, i=array.length-1) { + if (!array.length) {return 0;} + total += array[i]; + i++; + sum(array.slice(1), total, i); + return total; }; // 3. Sum all numbers in an array containing nested arrays. // Example: arraySum([1,[2,3],[[4]],5]); // 15 var arraySum = function(array) { + }; // 4. Check if a number is even. var isEven = function(n) { + }; // 5. Sum all integers below a given integer. // sumBelow(10); // 45 // sumBelow(7); // 21 var sumBelow = function(n) { + }; // 6. Get the integers in range (x, y). // Example: range(2, 9); // [3, 4, 5, 6, 7, 8] var range = function(x, y) { + }; // 7. Compute the exponent of a number. From 4ceada6bed1bd449380a66e4a5b0a38730a35ef8 Mon Sep 17 00:00:00 2001 From: "jonah.m.delaney@gmail.com" Date: Fri, 21 Sep 2018 15:59:42 -0500 Subject: [PATCH 2/7] finished sum, sumBelow, isEven, and started range --- src/recursion.js | 122 ++++++++++++++++++++++++++++++----------------- 1 file changed, 77 insertions(+), 45 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 194e77fb9..47a0ee80e 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -4,9 +4,9 @@ // denoted by n!, is the product of all positive integers less than or equal to n. // Example: 5! = 5 x 4 x 3 x 2 x 1 = 120 // factorial(5); // 120 -var factorial = function(n, sum=1, i=n) { - if (n < 0) {return null;} - if (i === 0) {return sum;} +var factorial = function (n, sum = 1, i = n) { + if (n < 0) { return null; } + if (i === 0) { return sum; } sum *= i; i--; return factorial(n - 1, sum); @@ -14,36 +14,68 @@ var factorial = function(n, sum=1, i=n) { // 2. Compute the sum of an array of integers. // Example: sum([1, 2, 3, 4, 5, 6]); // 21 -var sum = function(array, total=0, i=array.length-1) { - if (!array.length) {return 0;} +var sum = function (array, total = 0, i = 0) { + if (i >= array.length) { + return total; + } total += array[i]; i++; - sum(array.slice(1), total, i); - return total; + return sum(array, total, i); }; // 3. Sum all numbers in an array containing nested arrays. // Example: arraySum([1,[2,3],[[4]],5]); // 15 -var arraySum = function(array) { +var arraySum = function (array) { }; // 4. Check if a number is even. -var isEven = function(n) { - +var isEven = function (n, i = n) { + if (i - 2 === 0) { + return boolean = true; + } else if (i - 2 > 0) { + i -= 2; + boolean = false; + return isEven(n, i, boolean); + } else if (i + 2 === 0) { + return boolean = true; + } else if (i + 2 < 0) { + i += 2; + boolean = false; + return isEven(n, i, boolean); + } + return boolean; }; // 5. Sum all integers below a given integer. // sumBelow(10); // 45 // sumBelow(7); // 21 -var sumBelow = function(n) { - +var sumBelow = function (n, sum = 0, i = n) { + if (n === 0) { return sum; } + if (n > 0) { + i = n - 1; + sum += i; + i--; + return sumBelow(n - 1, sum, i); + } else if (n < 0) { + i = n + 1; + sum += i; + i++; + return sumBelow(n + 1, sum, i); + } + return sum; }; // 6. Get the integers in range (x, y). // Example: range(2, 9); // [3, 4, 5, 6, 7, 8] -var range = function(x, y) { - +var range = function (x, y, arr = []) { + if (x === y) { return arr;} + if (x !== y) { + x++; + arr.push(x); + return range(x, y, arr); + } + return arr; }; // 7. Compute the exponent of a number. @@ -51,22 +83,22 @@ var range = function(x, y) { // 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent. // Example: exponent(4,3); // 64 // https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/computing-powers-of-a-number -var exponent = function(base, exp) { +var exponent = function (base, exp) { }; // 8. Determine if a number is a power of two. // powerOfTwo(1); // true // powerOfTwo(16); // true // powerOfTwo(10); // false -var powerOfTwo = function(n) { +var powerOfTwo = function (n) { }; // 9. Write a function that accepts a string a reverses it. -var reverse = function(string) { +var reverse = function (string) { }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string) { +var palindrome = function (string) { }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -74,17 +106,17 @@ var palindrome = function(string) { // modulo(5,2) // 1 // modulo(17,5) // 2 // modulo(22,6) // 4 -var modulo = function(x, y) { +var modulo = function (x, y) { }; // 12. Write a function that multiplies two numbers without using the * operator or // JavaScript's Math object. -var multiply = function(x, y) { +var multiply = function (x, y) { }; // 13. Write a function that divides two numbers without using the / operator or // JavaScript's Math object. -var divide = function(x, y) { +var divide = function (x, y) { }; // 14. Find the greatest common divisor (gcd) of two positive numbers. The GCD of two @@ -92,7 +124,7 @@ var divide = function(x, y) { // Example: gcd(4,36); // 4 // http://www.cse.wustl.edu/~kjg/cse131/Notes/Recursion/recursion.html // https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm -var gcd = function(x, y) { +var gcd = function (x, y) { }; // 15. Write a function that compares each character of two strings and returns true if @@ -100,12 +132,12 @@ var gcd = function(x, y) { // compareStr('house', 'houses') // false // compareStr('', '') // true // compareStr('tomato', 'tomato') // true -var compareStr = function(str1, str2) { +var compareStr = function (str1, str2) { }; // 16. Write a function that accepts a string and creates an array where each letter // occupies an index of the array. -var createArray = function(str){ +var createArray = function (str) { }; // 17. Reverse the order of an array @@ -115,37 +147,37 @@ var reverseArr = function (array) { // 18. Create a new array with a given value and length. // buildList(0,5) // [0,0,0,0,0] // buildList(7,3) // [7,7,7] -var buildList = function(value, length) { +var buildList = function (value, length) { }; // 19. Count the occurence of a value inside a list. // countOccurrence([2,7,4,4,1,4], 4) // 3 // countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2 -var countOccurrence = function(array, value) { +var countOccurrence = function (array, value) { }; // 20. Write a recursive version of map. // rMap([1,2,3], timesTwo); // [2,4,6] -var rMap = function(array, callback) { +var rMap = function (array, callback) { }; // 21. Write a function that counts the number of times a key occurs in an object. // var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; // countKeysInObj(testobj, 'r') // 1 // countKeysInObj(testobj, 'e') // 2 -var countKeysInObj = function(obj, key) { +var countKeysInObj = function (obj, key) { }; // 22. Write a function that counts the number of times a value occurs in an object. // var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; // countValuesInObj(testobj, 'r') // 2 // countValuesInObj(testobj, 'e') // 1 -var countValuesInObj = function(obj, value) { +var countValuesInObj = function (obj, value) { }; // 23. Find all keys in an object (and nested objects) by a provided name and rename // them to a provided new name while preserving the value stored at that key. -var replaceKeysInObj = function(obj, key, newKey) { +var replaceKeysInObj = function (obj, key, newKey) { }; // 24. Get the first n Fibonacci numbers. In the Fibonacci Sequence, each subsequent @@ -153,7 +185,7 @@ var replaceKeysInObj = function(obj, key, newKey) { // Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..... // fibonacci(5); // [0, 1, 1, 2, 3, 5] // Note: The 0 is not counted. -var fibonacci = function(n) { +var fibonacci = function (n) { }; // 25. Return the Fibonacci number located at index n of the Fibonacci sequence. @@ -161,18 +193,18 @@ var fibonacci = function(n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function(n) { +var nthFibo = function (n) { }; // 26. Given an array of words, return a new array containing each word capitalized. // var words = ['i', 'am', 'learning', 'recursion']; // capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION'] -var capitalizeWords = function(input) { +var capitalizeWords = function (input) { }; // 27. Given an array of strings, capitalize the first letter of each index. // capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] -var capitalizeFirst = function(array) { +var capitalizeFirst = function (array) { }; // 28. Return the sum of all even numbers in an object containing nested objects. @@ -184,17 +216,17 @@ var capitalizeFirst = function(array) { // e: {e: {e: 2}, ee: 'car'} // }; // nestedEvenSum(obj1); // 10 -var nestedEvenSum = function(obj) { +var nestedEvenSum = function (obj) { }; // 29. Flatten an array containing nested arrays. // Example: flatten([1,[2],[3,[[4]]],5]); // [1,2,3,4,5] -var flatten = function(arrays) { +var flatten = function (arrays) { }; // 30. Given a string, return an object containing tallies of each letter. // letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1} -var letterTally = function(str, obj) { +var letterTally = function (str, obj) { }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -202,51 +234,51 @@ var letterTally = function(str, obj) { // elements should not be changed. // Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5] // Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4] -var compress = function(list) { +var compress = function (list) { }; // 32. Augment every element in a list with a new value where each element is an array // itself. // Example: augmentElements([[],[3],[7]], 5); // [[5],[3,5],[7,5]] -var augmentElements = function(array, aug) { +var augmentElements = function (array, aug) { }; // 33. Reduce a series of zeroes to a single 0. // minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4] // minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4] -var minimizeZeroes = function(array) { +var minimizeZeroes = function (array) { }; // 34. Alternate the numbers in an array between positive and negative regardless of // their original sign. The first number in the index always needs to be positive. // alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] // alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] -var alternateSign = function(array) { +var alternateSign = function (array) { }; // 35. Given a string, return a string with digits converted to their word equivalent. // Assume all numbers are single digits (less than 10). // numToText("I have 5 dogs and 6 ponies"); // "I have five dogs and six ponies" -var numToText = function(str) { +var numToText = function (str) { }; // *** EXTRA CREDIT *** // 36. Return the number of times a tag occurs in the DOM. -var tagCount = function(tag, node) { +var tagCount = function (tag, node) { }; // 37. Write a function for binary search. // Sample array: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] // console.log(binarySearch(5)) will return '5' -var binarySearch = function(array, target, min, max) { +var binarySearch = function (array, target, min, max) { }; // 38. Write a merge sort function. // Sample array: [34,7,23,32,5,62] // Sample output: [5,7,23,32,34,62] -var mergeSort = function(array) { +var mergeSort = function (array) { }; From 4a75a3edc275e17f18af2d04ac3fbad3c7e5fd5b Mon Sep 17 00:00:00 2001 From: "jonah.m.delaney@gmail.com" Date: Mon, 24 Sep 2018 15:55:25 -0500 Subject: [PATCH 3/7] finished exponent, power of two, and started palindrome --- src/recursion.js | 50 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 47a0ee80e..32fd83b73 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -69,8 +69,16 @@ var sumBelow = function (n, sum = 0, i = n) { // 6. Get the integers in range (x, y). // Example: range(2, 9); // [3, 4, 5, 6, 7, 8] var range = function (x, y, arr = []) { - if (x === y) { return arr;} - if (x !== y) { + if (x === y) { return arr; } + if (x > y) { + if (y !== x - 1) { + x--; + arr.push(x); + return range(x, y, arr); + } + return arr; 0. + } + if (x !== y - 1) { x++; arr.push(x); return range(x, y, arr); @@ -83,22 +91,54 @@ var range = function (x, y, arr = []) { // 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent. // Example: exponent(4,3); // 64 // https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/computing-powers-of-a-number -var exponent = function (base, exp) { +var exponent = function (base, exp, i = exp, total = 1) { + if (exp === 0) { return 1; } + if (exp === 1) { return base; } + if (i === 0) { return total; } + if (i > 0) { + total *= base; + i--; + return exponent(base, exp, i, total); + } + if (i < 0) { + total /= base; + i++; + return exponent(base, exp, i, total); + } + return total; }; // 8. Determine if a number is a power of two. // powerOfTwo(1); // true // powerOfTwo(16); // true // powerOfTwo(10); // false -var powerOfTwo = function (n) { +var powerOfTwo = function (n, x = 1) { + if (x === n) { return true; } + if (x > n) { return false; } + if (x < n) { + x *= 2; + return powerOfTwo(n, x); + } }; // 9. Write a function that accepts a string a reverses it. var reverse = function (string) { + if (string === "") {return "";} + else { + return reverse(string.substr(1)) + string.charAt(0); + } }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function (string) { +var palindrome = function (string, i = 0) { + if (string = reverse(string)) { + return true; + } + if (i <= 3) { + i++; + return palindrome(string, i); + } + return false; }; // 11. Write a function that returns the remainder of x divided by y without using the From a4b39265e93795de44812404fb2dd2b79325e239 Mon Sep 17 00:00:00 2001 From: "jonah.m.delaney@gmail.com" Date: Tue, 25 Sep 2018 15:57:17 -0500 Subject: [PATCH 4/7] finished a lot --- src/recursion.js | 108 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 19 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 32fd83b73..279283ace 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -123,22 +123,27 @@ var powerOfTwo = function (n, x = 1) { // 9. Write a function that accepts a string a reverses it. var reverse = function (string) { - if (string === "") {return "";} + if (string === "") { return ""; } else { return reverse(string.substr(1)) + string.charAt(0); } }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function (string, i = 0) { - if (string = reverse(string)) { - return true; - } - if (i <= 3) { +var palindrome = function (string, boolean = false, reverseString = function (string) { + if (string === "") { return ""; } + else { return reverseString(string.substr(1)) + string.charAt(0); } +}, i = 0) { + + if (string.charAt(0) === reverseString(string).charAt(0 + i)) { i++; - return palindrome(string, i); + return palindrome(string.slice(1)); + } else if (string.charAt(0) !== reverseString(string).charAt(0)) { + return true; } - return false; + // [1, 2, 3, 4, 5, 6] + // i = 0; + // x = string.length - (i + 1); }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -151,7 +156,20 @@ var modulo = function (x, y) { // 12. Write a function that multiplies two numbers without using the * operator or // JavaScript's Math object. -var multiply = function (x, y) { +var multiply = function (x, y, i = y, total = 0) { + if (x === 0 || y === 0) { return 0; } + if (i === 0 && y > 0) { return total; } + if (i === 0 && y < 0) { return -total; } + if (y > 0) { + total += x; + i--; + return multiply(x, y, i, total); + } + if (y < 0) { + total += x; + i++; + return multiply(x, y, i, total); + } }; // 13. Write a function that divides two numbers without using the / operator or @@ -172,33 +190,71 @@ var gcd = function (x, y) { // compareStr('house', 'houses') // false // compareStr('', '') // true // compareStr('tomato', 'tomato') // true -var compareStr = function (str1, str2) { +var compareStr = function (str1, str2, i = 0) { + if (i === str1.length && i === str2.length) { return true; } + if (str1.charAt(i) !== str2.charAt(i)) { + return false; + } else if (str1.charAt(i) === str2.charAt(i)) { + i++; + return compareStr(str1, str2, i); + } + return true; }; // 16. Write a function that accepts a string and creates an array where each letter // occupies an index of the array. -var createArray = function (str) { +var createArray = function (string, i = 0, array = []) { + if (i === string.length) { return array; } + if (string.charAt(i)) { + array.push(string.charAt(i)); + i++; + return createArray(string, i, array); + } + return array; }; // 17. Reverse the order of an array -var reverseArr = function (array) { +var reverseArr = function (array, i = array.length - 1, newArr = []) { + if (i === -1) { return newArr; } + if (i >= 0) { + newArr.push(array[i]); + i--; + return reverseArr(array, i, newArr); + } }; // 18. Create a new array with a given value and length. // buildList(0,5) // [0,0,0,0,0] // buildList(7,3) // [7,7,7] -var buildList = function (value, length) { +var buildList = function (value, length, i = 0, array = []) { + if (i === length) { return array; } + array.push(value); + i++; + return buildList(value, length, i, array); }; // 19. Count the occurence of a value inside a list. // countOccurrence([2,7,4,4,1,4], 4) // 3 // countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2 -var countOccurrence = function (array, value) { +var countOccurrence = function (array, value, i = 0, x = 0) { + if (i === array.length) { return x; } + if (array[i] === value) { + i++; + x++; + return countOccurrence(array, value, i, x); + } else { + i++; + return countOccurrence(array, value, i, x); + } }; // 20. Write a recursive version of map. // rMap([1,2,3], timesTwo); // [2,4,6] -var rMap = function (array, callback) { +var rMap = function (array, callback, i = 0, newArr = []) { + if (i === array.length) { return newArr; } + newArr.push(callback(array[i])); + i++; + return rMap(array, callback, i, newArr); }; // 21. Write a function that counts the number of times a key occurs in an object. @@ -233,18 +289,32 @@ var fibonacci = function (n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function (n) { +var nthFibo = function (n, i = 0, array = [0, 1]) { + if (i === n) { return array[i]; } + if (n < 0) { return null; } + array.push(array[i] + array[i + 1]); + i++; + return nthFibo(n, i, array) }; // 26. Given an array of words, return a new array containing each word capitalized. // var words = ['i', 'am', 'learning', 'recursion']; // capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION'] -var capitalizeWords = function (input) { -}; +var capitalizeWords = function (input, array = [], i = 0) { + if (input.length === array.length) { return array; } + array.push(input[i].toUpperCase()); + i++; + return capitalizeWords(input, array, i); +} // 27. Given an array of strings, capitalize the first letter of each index. // capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] -var capitalizeFirst = function (array) { +var capitalizeFirst = function (array, newArr = [], i = 0) { + if (array.length === newArr.length) {return array;} + newArr.push(array[i]); + newArr.charAt(0).toUpperCase(); + i++; + return capitalizeFirst(array, newArr, i); }; // 28. Return the sum of all even numbers in an object containing nested objects. From 275556b64fe53a540e32c3dfe520446f38507674 Mon Sep 17 00:00:00 2001 From: "jonah.m.delaney@gmail.com" Date: Wed, 26 Sep 2018 15:55:37 -0500 Subject: [PATCH 5/7] we done did a whole lot thanks to a duck... --- src/recursion.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 279283ace..62c4d9a66 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -310,9 +310,8 @@ var capitalizeWords = function (input, array = [], i = 0) { // 27. Given an array of strings, capitalize the first letter of each index. // capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] var capitalizeFirst = function (array, newArr = [], i = 0) { - if (array.length === newArr.length) {return array;} - newArr.push(array[i]); - newArr.charAt(0).toUpperCase(); + if (array.length === newArr.length) { return newArr; } + newArr.push(array[i].charAt(0).toUpperCase() + array[i].slice(1)); i++; return capitalizeFirst(array, newArr, i); }; @@ -336,7 +335,22 @@ var flatten = function (arrays) { // 30. Given a string, return an object containing tallies of each letter. // letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1} -var letterTally = function (str, obj) { +var letterTally = function (string, object = {}, array = [], i = 0, key = array[i]) { + if (i === string.length) { return object; } + if (string.length > array.length) { + array.push(string.charAt(i)); + i++; + return letterTally(string, object, array, i, array[i]); + } + + if (array.length === string.length) { + i = 0; + object[key] = countOccurrence(array, array[i], i); + return letterTally(string, object, array, i, array[i]); + } + +return object; + }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -363,7 +377,7 @@ var minimizeZeroes = function (array) { // their original sign. The first number in the index always needs to be positive. // alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] // alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] -var alternateSign = function (array) { +var alternateSign = function (array, i = 1, newArr = [array[0]]) { }; // 35. Given a string, return a string with digits converted to their word equivalent. From 54de4835c3d3c19c6b836d918e46b19d20f199a3 Mon Sep 17 00:00:00 2001 From: JonahDelaney Date: Wed, 26 Sep 2018 20:32:48 -0500 Subject: [PATCH 6/7] Add files via upload --- recursion.js | 464 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 464 insertions(+) create mode 100644 recursion.js diff --git a/recursion.js b/recursion.js new file mode 100644 index 000000000..e876a0557 --- /dev/null +++ b/recursion.js @@ -0,0 +1,464 @@ +// Solve all of the following prompts using recursion. + +// 1. Calculate the factorial of a number. The factorial of a non-negative integer n, +// denoted by n!, is the product of all positive integers less than or equal to n. +// Example: 5! = 5 x 4 x 3 x 2 x 1 = 120 +// factorial(5); // 120 +var factorial = function (n, sum = 1, i = n) { + if (n < 0) { return null; } + if (i === 0) { return sum; } + sum *= i; + i--; + return factorial(n - 1, sum); +}; + +// 2. Compute the sum of an array of integers. +// Example: sum([1, 2, 3, 4, 5, 6]); // 21 +var sum = function (array, total = 0, i = 0) { + if (i >= array.length) { + return total; + } + total += array[i]; + i++; + return sum(array, total, i); +}; + +// 3. Sum all numbers in an array containing nested arrays. +// Example: arraySum([1,[2,3],[[4]],5]); // 15 +var arraySum = function (array) { + +}; + +// 4. Check if a number is even. +var isEven = function (n, i = n) { + if (i - 2 === 0) { + return boolean = true; + } else if (i - 2 > 0) { + i -= 2; + boolean = false; + return isEven(n, i, boolean); + } else if (i + 2 === 0) { + return boolean = true; + } else if (i + 2 < 0) { + i += 2; + boolean = false; + return isEven(n, i, boolean); + } + return boolean; +}; + +// 5. Sum all integers below a given integer. +// sumBelow(10); // 45 +// sumBelow(7); // 21 +var sumBelow = function (n, sum = 0, i = n) { + if (n === 0) { return sum; } + if (n > 0) { + i = n - 1; + sum += i; + i--; + return sumBelow(n - 1, sum, i); + } else if (n < 0) { + i = n + 1; + sum += i; + i++; + return sumBelow(n + 1, sum, i); + } + return sum; +}; + +// 6. Get the integers in range (x, y). +// Example: range(2, 9); // [3, 4, 5, 6, 7, 8] +var range = function (x, y, arr = []) { + if (x === y) { return arr; } + if (x > y) { + if (y !== x - 1) { + x--; + arr.push(x); + return range(x, y, arr); + } + return arr; 0. + } + if (x !== y - 1) { + x++; + arr.push(x); + return range(x, y, arr); + } + return arr; +}; + +// 7. Compute the exponent of a number. +// The exponent of a number says how many times the base number is used as a factor. +// 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent. +// Example: exponent(4,3); // 64 +// https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/computing-powers-of-a-number +var exponent = function (base, exp, i = exp, total = 1) { + if (exp === 0) { return 1; } + if (exp === 1) { return base; } + if (i === 0) { return total; } + if (i > 0) { + total *= base; + i--; + return exponent(base, exp, i, total); + } + if (i < 0) { + total /= base; + i++; + return exponent(base, exp, i, total); + } + return total; +}; + +// 8. Determine if a number is a power of two. +// powerOfTwo(1); // true +// powerOfTwo(16); // true +// powerOfTwo(10); // false +var powerOfTwo = function (n, x = 1) { + if (x === n) { return true; } + if (x > n) { return false; } + if (x < n) { + x *= 2; + return powerOfTwo(n, x); + } +}; + +// 9. Write a function that accepts a string a reverses it. +var reverse = function (string) { + if (string === "") { return ""; } + else { + return reverse(string.substr(1)) + string.charAt(0); + } +}; + +// 10. Write a function that determines if a string is a palindrome. +var palindrome = function (string, i = string.length - 1, back = "") { + string = string.replace(" ", ""); + back = back.replace(" ", ""); + if (i < 0) { + if (back.toLowerCase() == string.toLowerCase()) { + return true; + } else { + return false; + } + } + back = back.concat(string.charAt(i)); + i--; + return palindrome(string, i, back); +}; + +// 11. Write a function that returns the remainder of x divided by y without using the +// modulo (%) operator. +// modulo(5,2) // 1 +// modulo(17,5) // 2 +// modulo(22,6) // 4 +var modulo = function (x, y) { +}; + +// 12. Write a function that multiplies two numbers without using the * operator or +// JavaScript's Math object. +var multiply = function (x, y, i = y, total = 0) { + if (x === 0 || y === 0) { return 0; } + if (i === 0 && y > 0) { return total; } + if (i === 0 && y < 0) { return -total; } + if (y > 0) { + total += x; + i--; + return multiply(x, y, i, total); + } + if (y < 0) { + total += x; + i++; + return multiply(x, y, i, total); + } +}; + +// 13. Write a function that divides two numbers without using the / operator or +// JavaScript's Math object. +var divide = function (x, y) { +}; + +// 14. Find the greatest common divisor (gcd) of two positive numbers. The GCD of two +// integers is the greatest integer that divides both x and y with no remainder. +// Example: gcd(4,36); // 4 +// http://www.cse.wustl.edu/~kjg/cse131/Notes/Recursion/recursion.html +// https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm +var gcd = function (x, y) { +}; + +// 15. Write a function that compares each character of two strings and returns true if +// both are identical. +// compareStr('house', 'houses') // false +// compareStr('', '') // true +// compareStr('tomato', 'tomato') // true +var compareStr = function (str1, str2, i = 0) { + if (i === str1.length && i === str2.length) { return true; } + if (str1.charAt(i) !== str2.charAt(i)) { + return false; + } else if (str1.charAt(i) === str2.charAt(i)) { + i++; + return compareStr(str1, str2, i); + } + return true; +}; + +// 16. Write a function that accepts a string and creates an array where each letter +// occupies an index of the array. +var createArray = function (string, i = 0, array = []) { + if (i === string.length) { return array; } + if (string.charAt(i)) { + array.push(string.charAt(i)); + i++; + return createArray(string, i, array); + } + return array; +}; + +// 17. Reverse the order of an array +var reverseArr = function (array, i = array.length - 1, newArr = []) { + if (i === -1) { return newArr; } + if (i >= 0) { + newArr.push(array[i]); + i--; + return reverseArr(array, i, newArr); + } +}; + +// 18. Create a new array with a given value and length. +// buildList(0,5) // [0,0,0,0,0] +// buildList(7,3) // [7,7,7] +var buildList = function (value, length, i = 0, array = []) { + if (i === length) { return array; } + array.push(value); + i++; + return buildList(value, length, i, array); +}; + +// 19. Count the occurence of a value inside a list. +// countOccurrence([2,7,4,4,1,4], 4) // 3 +// countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2 +var countOccurrence = function (array, value, i = 0, x = 0) { + if (i === array.length) { return x; } + if (array[i] === value) { + i++; + x++; + return countOccurrence(array, value, i, x); + } else { + i++; + return countOccurrence(array, value, i, x); + } +}; + +// 20. Write a recursive version of map. +// rMap([1,2,3], timesTwo); // [2,4,6] +var rMap = function (array, callback, i = 0, newArr = []) { + if (i === array.length) { return newArr; } + newArr.push(callback(array[i])); + i++; + return rMap(array, callback, i, newArr); +}; + +// 21. Write a function that counts the number of times a key occurs in an object. +// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; +// countKeysInObj(testobj, 'r') // 1 +// countKeysInObj(testobj, 'e') // 2 +var countKeysInObj = function (obj, key) { +}; + +// 22. Write a function that counts the number of times a value occurs in an object. +// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; +// countValuesInObj(testobj, 'r') // 2 +// countValuesInObj(testobj, 'e') // 1 +var countValuesInObj = function (obj, value) { +}; + +// 23. Find all keys in an object (and nested objects) by a provided name and rename +// them to a provided new name while preserving the value stored at that key. +var replaceKeysInObj = function (obj, key, newKey) { +}; + +// 24. Get the first n Fibonacci numbers. In the Fibonacci Sequence, each subsequent +// number is the sum of the previous two. +// Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..... +// fibonacci(5); // [0, 1, 1, 2, 3, 5] +// Note: The 0 is not counted. +var fibonacci = function (n) { +}; + +// 25. Return the Fibonacci number located at index n of the Fibonacci sequence. +// [0,1,1,2,3,5,8,13,21] +// nthFibo(5); // 5 +// nthFibo(7); // 13 +// nthFibo(3); // 2 +var nthFibo = function (n, i = 0, array = [0, 1]) { + if (i === n) { return array[i]; } + if (n < 0) { return null; } + array.push(array[i] + array[i + 1]); + i++; + return nthFibo(n, i, array) +}; + +// 26. Given an array of words, return a new array containing each word capitalized. +// var words = ['i', 'am', 'learning', 'recursion']; +// capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION'] +var capitalizeWords = function (input, array = [], i = 0) { + if (input.length === array.length) { return array; } + array.push(input[i].toUpperCase()); + i++; + return capitalizeWords(input, array, i); +} + +// 27. Given an array of strings, capitalize the first letter of each index. +// capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] +var capitalizeFirst = function (array, newArr = [], i = 0) { + if (array.length === newArr.length) { return newArr; } + newArr.push(array[i].charAt(0).toUpperCase() + array[i].slice(1)); + i++; + return capitalizeFirst(array, newArr, i); +}; + +// 28. Return the sum of all even numbers in an object containing nested objects. +// var obj1 = { +// a: 2, +// b: {b: 2, bb: {b: 3, bb: {b: 2}}}, +// c: {c: {c: 2}, cc: 'ball', ccc: 5}, +// d: 1, +// e: {e: {e: 2}, ee: 'car'} +// }; +// nestedEvenSum(obj1); // 10 +var nestedEvenSum = function (obj) { +}; + +// 29. Flatten an array containing nested arrays. +// Example: flatten([1,[2],[3,[[4]]],5]); // [1,2,3,4,5] +var flatten = function (arrays) { +}; + +// 30. Given a string, return an object containing tallies of each letter. +// letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1} +var letterTally = function (str, obj = {}, i = 0) { + if (i === str.length) { return obj; } + if (isNaN(obj[str[i]])) { + obj[str[i]] = 0; + } + obj[str[i]] += 1; + i++; + return letterTally(str, obj, i); +}; + + + +// 31. Eliminate consecutive duplicates in a list. If the list contains repeated +// elements they should be replaced with a single copy of the element. The order of the +// elements should not be changed. +// Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5] +// Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4] +var compress = function (list, arr = [], i = 0) { + if (i === list.length) { return arr; } + if (list[i] != arr[arr.length - 1]) { + arr.push(list[i]); + } + i++; + return compress(list, arr, i); +}; + +// 32. Augment every element in a list with a new value where each element is an array +// itself. +// Example: augmentElements([[],[3],[7]], 5); // [[5],[3,5],[7,5]] +var augmentElements = function (array, aug) { +}; + +// 33. Reduce a series of zeroes to a single 0. +// minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4] +// minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4] +var minimizeZeroes = function (array, arr = [], i = 0) { + if (i === array.length) { return arr; } + if (array[i] !== 0) { + arr.push(array[i]); + } else if (array[i] === 0 && arr[arr.length - 1] != 0) { + arr.push(array[i]); + } + i++; + return minimizeZeroes(array, arr, i); +}; + +// 34. Alternate the numbers in an array between positive and negative regardless of +// their original sign. The first number in the index always needs to be positive. +// alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] +// alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] +var alternateSign = function (array, newArr = [], i = 0, sign = 1) { + if (array.length === newArr.length) { return newArr; } + newArr.push(array[i]); + if (newArr[i] > 0) { + newArr[i] *= sign; + i++; + return alternateSign(array, newArr, i, -sign); + } else if (newArr[i] === 0) { + i++; + return alternateSign(array, newArr, i, -sign); + } else { + newArr[i] *= -sign; + i++; + return alternateSign(array, newArr, i, -sign); + } +}; + +// 35. Given a string, return a string with digits converted to their word equivalent. +// Assume all numbers are single digits (less than 10). +// numToText("I have 5 dogs and 6 ponies"); // "I have five dogs and six ponies" +var numToText = function (str) { +}; + +// *** EXTRA CREDIT *** + +// 36. Return the number of times a tag occurs in the DOM. +var tagCount = function (tag, node) { +}; + +// 37. Write a function for binary search. +// Sample array: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] +// console.log(binarySearch(5)) will return '5' + +var binarySearch = function (array, target, min, max) { +}; + +// 38. Write a merge sort function. +// Sample array: [34,7,23,32,5,62] +// Sample output: [5,7,23,32,34,62] +var mergeSort = function (array) { +}; + + + +//----------------------------------- +// DON'T REMOVE THIS CODE ----------- +//----------------------------------- + +if ((typeof process !== 'undefined') && + (typeof process.versions.node !== 'undefined')) { + module.exports = { + factorial, + sum, + isEven, + sumBelow, + range, + exponent, + powerOfTwo, + reverse, + palindrome, + multiply, + compareStr, + createArray, + reverseArr, + buildList, + countOccurrence, + rMap, + nthFibo, + capitalizeWords, + capitalizeFirst, + letterTally, + compress, + minimizeZeroes, + alternateSign, + }; +} + +//----------------------------------- \ No newline at end of file From 83f8812206b3b50d370be7109de3c4d4269e0655 Mon Sep 17 00:00:00 2001 From: Half Day Date: Thu, 1 Nov 2018 13:58:47 -0500 Subject: [PATCH 7/7] finished recursion (sorry i was late) --- .DS_Store | Bin 0 -> 6148 bytes recursion.js | 464 ----------------------------------------------- src/recursion.js | 74 ++++---- 3 files changed, 42 insertions(+), 496 deletions(-) create mode 100644 .DS_Store delete mode 100644 recursion.js diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..add8e05d3b544664bb51b8e0885c34a31c398e7d GIT binary patch literal 6148 zcmeHKO^ee&7=C9PHQ6rO1;G_O1U%?rUAk zc=arL^#lKg`Xl@w;#r^haA~vdRYc~6ndi;S`@G3Klgwm@h;_4PQl`x*vkrIg)%(h zfJ$JFvEkXk!&6px-zz)=(Id`Wj3cha>p3kB(*m;n2z1TE6Tj%W&OqlHwr@E%XXun8195gFAfjFQ4*x%PFf~GF5*>k;otX)A@Z0P9e)o)AhJxW-&mVW zTJ5XrcB{2HUAHIgb`$>AwdvF{&GVOTZ9geryn6HY-TMz8KYtM{Vp!gVtt$KtU*Q~z zao}c2oF@BdX4IGl_zNN^Qr&;al+< ciZJxq+yOcYXA;o@(?0@223=_d{;C4M0Dt(`$^ZZW literal 0 HcmV?d00001 diff --git a/recursion.js b/recursion.js deleted file mode 100644 index e876a0557..000000000 --- a/recursion.js +++ /dev/null @@ -1,464 +0,0 @@ -// Solve all of the following prompts using recursion. - -// 1. Calculate the factorial of a number. The factorial of a non-negative integer n, -// denoted by n!, is the product of all positive integers less than or equal to n. -// Example: 5! = 5 x 4 x 3 x 2 x 1 = 120 -// factorial(5); // 120 -var factorial = function (n, sum = 1, i = n) { - if (n < 0) { return null; } - if (i === 0) { return sum; } - sum *= i; - i--; - return factorial(n - 1, sum); -}; - -// 2. Compute the sum of an array of integers. -// Example: sum([1, 2, 3, 4, 5, 6]); // 21 -var sum = function (array, total = 0, i = 0) { - if (i >= array.length) { - return total; - } - total += array[i]; - i++; - return sum(array, total, i); -}; - -// 3. Sum all numbers in an array containing nested arrays. -// Example: arraySum([1,[2,3],[[4]],5]); // 15 -var arraySum = function (array) { - -}; - -// 4. Check if a number is even. -var isEven = function (n, i = n) { - if (i - 2 === 0) { - return boolean = true; - } else if (i - 2 > 0) { - i -= 2; - boolean = false; - return isEven(n, i, boolean); - } else if (i + 2 === 0) { - return boolean = true; - } else if (i + 2 < 0) { - i += 2; - boolean = false; - return isEven(n, i, boolean); - } - return boolean; -}; - -// 5. Sum all integers below a given integer. -// sumBelow(10); // 45 -// sumBelow(7); // 21 -var sumBelow = function (n, sum = 0, i = n) { - if (n === 0) { return sum; } - if (n > 0) { - i = n - 1; - sum += i; - i--; - return sumBelow(n - 1, sum, i); - } else if (n < 0) { - i = n + 1; - sum += i; - i++; - return sumBelow(n + 1, sum, i); - } - return sum; -}; - -// 6. Get the integers in range (x, y). -// Example: range(2, 9); // [3, 4, 5, 6, 7, 8] -var range = function (x, y, arr = []) { - if (x === y) { return arr; } - if (x > y) { - if (y !== x - 1) { - x--; - arr.push(x); - return range(x, y, arr); - } - return arr; 0. - } - if (x !== y - 1) { - x++; - arr.push(x); - return range(x, y, arr); - } - return arr; -}; - -// 7. Compute the exponent of a number. -// The exponent of a number says how many times the base number is used as a factor. -// 8^2 = 8 x 8 = 64. Here, 8 is the base and 2 is the exponent. -// Example: exponent(4,3); // 64 -// https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/computing-powers-of-a-number -var exponent = function (base, exp, i = exp, total = 1) { - if (exp === 0) { return 1; } - if (exp === 1) { return base; } - if (i === 0) { return total; } - if (i > 0) { - total *= base; - i--; - return exponent(base, exp, i, total); - } - if (i < 0) { - total /= base; - i++; - return exponent(base, exp, i, total); - } - return total; -}; - -// 8. Determine if a number is a power of two. -// powerOfTwo(1); // true -// powerOfTwo(16); // true -// powerOfTwo(10); // false -var powerOfTwo = function (n, x = 1) { - if (x === n) { return true; } - if (x > n) { return false; } - if (x < n) { - x *= 2; - return powerOfTwo(n, x); - } -}; - -// 9. Write a function that accepts a string a reverses it. -var reverse = function (string) { - if (string === "") { return ""; } - else { - return reverse(string.substr(1)) + string.charAt(0); - } -}; - -// 10. Write a function that determines if a string is a palindrome. -var palindrome = function (string, i = string.length - 1, back = "") { - string = string.replace(" ", ""); - back = back.replace(" ", ""); - if (i < 0) { - if (back.toLowerCase() == string.toLowerCase()) { - return true; - } else { - return false; - } - } - back = back.concat(string.charAt(i)); - i--; - return palindrome(string, i, back); -}; - -// 11. Write a function that returns the remainder of x divided by y without using the -// modulo (%) operator. -// modulo(5,2) // 1 -// modulo(17,5) // 2 -// modulo(22,6) // 4 -var modulo = function (x, y) { -}; - -// 12. Write a function that multiplies two numbers without using the * operator or -// JavaScript's Math object. -var multiply = function (x, y, i = y, total = 0) { - if (x === 0 || y === 0) { return 0; } - if (i === 0 && y > 0) { return total; } - if (i === 0 && y < 0) { return -total; } - if (y > 0) { - total += x; - i--; - return multiply(x, y, i, total); - } - if (y < 0) { - total += x; - i++; - return multiply(x, y, i, total); - } -}; - -// 13. Write a function that divides two numbers without using the / operator or -// JavaScript's Math object. -var divide = function (x, y) { -}; - -// 14. Find the greatest common divisor (gcd) of two positive numbers. The GCD of two -// integers is the greatest integer that divides both x and y with no remainder. -// Example: gcd(4,36); // 4 -// http://www.cse.wustl.edu/~kjg/cse131/Notes/Recursion/recursion.html -// https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/the-euclidean-algorithm -var gcd = function (x, y) { -}; - -// 15. Write a function that compares each character of two strings and returns true if -// both are identical. -// compareStr('house', 'houses') // false -// compareStr('', '') // true -// compareStr('tomato', 'tomato') // true -var compareStr = function (str1, str2, i = 0) { - if (i === str1.length && i === str2.length) { return true; } - if (str1.charAt(i) !== str2.charAt(i)) { - return false; - } else if (str1.charAt(i) === str2.charAt(i)) { - i++; - return compareStr(str1, str2, i); - } - return true; -}; - -// 16. Write a function that accepts a string and creates an array where each letter -// occupies an index of the array. -var createArray = function (string, i = 0, array = []) { - if (i === string.length) { return array; } - if (string.charAt(i)) { - array.push(string.charAt(i)); - i++; - return createArray(string, i, array); - } - return array; -}; - -// 17. Reverse the order of an array -var reverseArr = function (array, i = array.length - 1, newArr = []) { - if (i === -1) { return newArr; } - if (i >= 0) { - newArr.push(array[i]); - i--; - return reverseArr(array, i, newArr); - } -}; - -// 18. Create a new array with a given value and length. -// buildList(0,5) // [0,0,0,0,0] -// buildList(7,3) // [7,7,7] -var buildList = function (value, length, i = 0, array = []) { - if (i === length) { return array; } - array.push(value); - i++; - return buildList(value, length, i, array); -}; - -// 19. Count the occurence of a value inside a list. -// countOccurrence([2,7,4,4,1,4], 4) // 3 -// countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2 -var countOccurrence = function (array, value, i = 0, x = 0) { - if (i === array.length) { return x; } - if (array[i] === value) { - i++; - x++; - return countOccurrence(array, value, i, x); - } else { - i++; - return countOccurrence(array, value, i, x); - } -}; - -// 20. Write a recursive version of map. -// rMap([1,2,3], timesTwo); // [2,4,6] -var rMap = function (array, callback, i = 0, newArr = []) { - if (i === array.length) { return newArr; } - newArr.push(callback(array[i])); - i++; - return rMap(array, callback, i, newArr); -}; - -// 21. Write a function that counts the number of times a key occurs in an object. -// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; -// countKeysInObj(testobj, 'r') // 1 -// countKeysInObj(testobj, 'e') // 2 -var countKeysInObj = function (obj, key) { -}; - -// 22. Write a function that counts the number of times a value occurs in an object. -// var testobj = {'e': {'x':'y'}, 't':{'r': {'e':'r'}, 'p': {'y':'r'}},'y':'e'}; -// countValuesInObj(testobj, 'r') // 2 -// countValuesInObj(testobj, 'e') // 1 -var countValuesInObj = function (obj, value) { -}; - -// 23. Find all keys in an object (and nested objects) by a provided name and rename -// them to a provided new name while preserving the value stored at that key. -var replaceKeysInObj = function (obj, key, newKey) { -}; - -// 24. Get the first n Fibonacci numbers. In the Fibonacci Sequence, each subsequent -// number is the sum of the previous two. -// Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34..... -// fibonacci(5); // [0, 1, 1, 2, 3, 5] -// Note: The 0 is not counted. -var fibonacci = function (n) { -}; - -// 25. Return the Fibonacci number located at index n of the Fibonacci sequence. -// [0,1,1,2,3,5,8,13,21] -// nthFibo(5); // 5 -// nthFibo(7); // 13 -// nthFibo(3); // 2 -var nthFibo = function (n, i = 0, array = [0, 1]) { - if (i === n) { return array[i]; } - if (n < 0) { return null; } - array.push(array[i] + array[i + 1]); - i++; - return nthFibo(n, i, array) -}; - -// 26. Given an array of words, return a new array containing each word capitalized. -// var words = ['i', 'am', 'learning', 'recursion']; -// capitalizedWords(words); // ['I', 'AM', 'LEARNING', 'RECURSION'] -var capitalizeWords = function (input, array = [], i = 0) { - if (input.length === array.length) { return array; } - array.push(input[i].toUpperCase()); - i++; - return capitalizeWords(input, array, i); -} - -// 27. Given an array of strings, capitalize the first letter of each index. -// capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] -var capitalizeFirst = function (array, newArr = [], i = 0) { - if (array.length === newArr.length) { return newArr; } - newArr.push(array[i].charAt(0).toUpperCase() + array[i].slice(1)); - i++; - return capitalizeFirst(array, newArr, i); -}; - -// 28. Return the sum of all even numbers in an object containing nested objects. -// var obj1 = { -// a: 2, -// b: {b: 2, bb: {b: 3, bb: {b: 2}}}, -// c: {c: {c: 2}, cc: 'ball', ccc: 5}, -// d: 1, -// e: {e: {e: 2}, ee: 'car'} -// }; -// nestedEvenSum(obj1); // 10 -var nestedEvenSum = function (obj) { -}; - -// 29. Flatten an array containing nested arrays. -// Example: flatten([1,[2],[3,[[4]]],5]); // [1,2,3,4,5] -var flatten = function (arrays) { -}; - -// 30. Given a string, return an object containing tallies of each letter. -// letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1} -var letterTally = function (str, obj = {}, i = 0) { - if (i === str.length) { return obj; } - if (isNaN(obj[str[i]])) { - obj[str[i]] = 0; - } - obj[str[i]] += 1; - i++; - return letterTally(str, obj, i); -}; - - - -// 31. Eliminate consecutive duplicates in a list. If the list contains repeated -// elements they should be replaced with a single copy of the element. The order of the -// elements should not be changed. -// Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5] -// Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4] -var compress = function (list, arr = [], i = 0) { - if (i === list.length) { return arr; } - if (list[i] != arr[arr.length - 1]) { - arr.push(list[i]); - } - i++; - return compress(list, arr, i); -}; - -// 32. Augment every element in a list with a new value where each element is an array -// itself. -// Example: augmentElements([[],[3],[7]], 5); // [[5],[3,5],[7,5]] -var augmentElements = function (array, aug) { -}; - -// 33. Reduce a series of zeroes to a single 0. -// minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4] -// minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4] -var minimizeZeroes = function (array, arr = [], i = 0) { - if (i === array.length) { return arr; } - if (array[i] !== 0) { - arr.push(array[i]); - } else if (array[i] === 0 && arr[arr.length - 1] != 0) { - arr.push(array[i]); - } - i++; - return minimizeZeroes(array, arr, i); -}; - -// 34. Alternate the numbers in an array between positive and negative regardless of -// their original sign. The first number in the index always needs to be positive. -// alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] -// alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] -var alternateSign = function (array, newArr = [], i = 0, sign = 1) { - if (array.length === newArr.length) { return newArr; } - newArr.push(array[i]); - if (newArr[i] > 0) { - newArr[i] *= sign; - i++; - return alternateSign(array, newArr, i, -sign); - } else if (newArr[i] === 0) { - i++; - return alternateSign(array, newArr, i, -sign); - } else { - newArr[i] *= -sign; - i++; - return alternateSign(array, newArr, i, -sign); - } -}; - -// 35. Given a string, return a string with digits converted to their word equivalent. -// Assume all numbers are single digits (less than 10). -// numToText("I have 5 dogs and 6 ponies"); // "I have five dogs and six ponies" -var numToText = function (str) { -}; - -// *** EXTRA CREDIT *** - -// 36. Return the number of times a tag occurs in the DOM. -var tagCount = function (tag, node) { -}; - -// 37. Write a function for binary search. -// Sample array: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] -// console.log(binarySearch(5)) will return '5' - -var binarySearch = function (array, target, min, max) { -}; - -// 38. Write a merge sort function. -// Sample array: [34,7,23,32,5,62] -// Sample output: [5,7,23,32,34,62] -var mergeSort = function (array) { -}; - - - -//----------------------------------- -// DON'T REMOVE THIS CODE ----------- -//----------------------------------- - -if ((typeof process !== 'undefined') && - (typeof process.versions.node !== 'undefined')) { - module.exports = { - factorial, - sum, - isEven, - sumBelow, - range, - exponent, - powerOfTwo, - reverse, - palindrome, - multiply, - compareStr, - createArray, - reverseArr, - buildList, - countOccurrence, - rMap, - nthFibo, - capitalizeWords, - capitalizeFirst, - letterTally, - compress, - minimizeZeroes, - alternateSign, - }; -} - -//----------------------------------- \ No newline at end of file diff --git a/src/recursion.js b/src/recursion.js index 62c4d9a66..7d5bb202c 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -130,21 +130,15 @@ var reverse = function (string) { }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function (string, boolean = false, reverseString = function (string) { - if (string === "") { return ""; } - else { return reverseString(string.substr(1)) + string.charAt(0); } -}, i = 0) { - - if (string.charAt(0) === reverseString(string).charAt(0 + i)) { - i++; - return palindrome(string.slice(1)); - } else if (string.charAt(0) !== reverseString(string).charAt(0)) { - return true; +var palindrome = function (string) { + if (string.length <= 1) { return true; } + if (string[0].toLowerCase() !== string[string.length - 1].toLowerCase()) { + return false; } - // [1, 2, 3, 4, 5, 6] - // i = 0; - // x = string.length - (i + 1); -}; + string = string.substr(1, string.length - 2); + string = string.replace(' ', ''); + return palindrome(string); +} // 11. Write a function that returns the remainder of x divided by y without using the // modulo (%) operator. @@ -335,30 +329,29 @@ var flatten = function (arrays) { // 30. Given a string, return an object containing tallies of each letter. // letterTally('potato'); // {'p':1, 'o':2, 't':2, 'a':1} -var letterTally = function (string, object = {}, array = [], i = 0, key = array[i]) { - if (i === string.length) { return object; } - if (string.length > array.length) { - array.push(string.charAt(i)); - i++; - return letterTally(string, object, array, i, array[i]); - } - - if (array.length === string.length) { - i = 0; - object[key] = countOccurrence(array, array[i], i); - return letterTally(string, object, array, i, array[i]); +var letterTally = function (str, obj = {}, tally = 0, i = 0) { + let letters = str.split(''); + let arrOfObjKeys = Object.keys(obj); + if (i === letters.length) { return obj; } + if (!obj[letters[i]]) { obj[letters[i]] = 1; } + else { + obj[letters[i]] += 1; } - -return object; - + i++; + return letterTally(str, obj, tally, i); }; + // 31. Eliminate consecutive duplicates in a list. If the list contains repeated // elements they should be replaced with a single copy of the element. The order of the // elements should not be changed. // Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5] // Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4] -var compress = function (list) { +var compress = function (list, arr = [], i = 0) { + if (i === list.length) { return arr; } + if (list[i] !== arr[arr.length - 1]) { arr.push(list[i]); } + i++; + return compress(list, arr, i); }; // 32. Augment every element in a list with a new value where each element is an array @@ -370,14 +363,31 @@ var augmentElements = function (array, aug) { // 33. Reduce a series of zeroes to a single 0. // minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4] // minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4] -var minimizeZeroes = function (array) { +var minimizeZeroes = function(array, arr = [], i= 0, n =1) { + if(i === array.length){ + return arr; + } + if(array[i] !== array[n]){ + arr.push(array[i]); + } + i++; + n++; + return minimizeZeroes(array, arr, i , n) }; + // 34. Alternate the numbers in an array between positive and negative regardless of // their original sign. The first number in the index always needs to be positive. // alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] // alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] -var alternateSign = function (array, i = 1, newArr = [array[0]]) { +var alternateSign = function (array, i = 0) { + i++; + var el = array[i]; + if (i === array.length) { return array; } + if (array[0] < 0) { array[0] *= -1; } + if (el > 0 && i % 2 === 1) { array[i] = array[i] * -1; } + if (el < 0 && i % 2 === 0) { array[i] *= -1; } + return alternateSign(array, i); }; // 35. Given a string, return a string with digits converted to their word equivalent.