From 23e94d9018cca0a07e8e206e00cc5f64f4d9c974 Mon Sep 17 00:00:00 2001 From: alexo12 Date: Thu, 20 Sep 2018 15:56:31 -0500 Subject: [PATCH 1/8] ope 0_0 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..4c813ff8f 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -4,12 +4,26 @@ // 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, total = 1) { +if(n < 0) { + return null; +}else if(n === 0) { + return total; +} +total *= n; +n--; + return factorial(n, total); }; // 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 = 1) { + for(let i = 0; i < array.length; i++) { + if (array === 0) { + return total; + } + + } }; // 3. Sum all numbers in an array containing nested arrays. 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 2/8] 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 3/8] 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 d16539d9a7db8ebb032f6b37d70b0b955d6f8b9c Mon Sep 17 00:00:00 2001 From: alexo12 Date: Fri, 21 Sep 2018 16:00:59 -0500 Subject: [PATCH 4/8] 0.0 --- src/recursion.js | 60 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 4c813ff8f..b9610e5b2 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -17,33 +17,71 @@ n--; // 2. Compute the sum of an array of integers. // Example: sum([1, 2, 3, 4, 5, 6]); // 21 -var sum = function(array, total = 1) { - for(let i = 0; i < array.length; i++) { - if (array === 0) { - return total; - } - - } -}; - +var sum = function(array, total = 0, i = 0) { + if(i >= array.length) { + return total; + } + total += array[i]; ccccvvvvvv + i++; + return sum(array, total, i); + +}; +/* +base - if there is nothing left in the array (!array.length) +run a loop to access array (loop) +total = array[0] + array[1] +recursive = sum(array.slice(1)) <--- starts the new loop on +the very next element + +*/ // 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) { +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) { + }; // 7. Compute the exponent of a number. From d41eff05b4db3c60b6154ccbb12270241824c791 Mon Sep 17 00:00:00 2001 From: alexo12 Date: Mon, 24 Sep 2018 15:55:18 -0500 Subject: [PATCH 5/8] worked on recursion --- src/recursion.js | 70 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index b9610e5b2..155f876e8 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -21,7 +21,7 @@ var sum = function(array, total = 0, i = 0) { if(i >= array.length) { return total; } - total += array[i]; ccccvvvvvv + total += array[i]; i++; return sum(array, total, i); @@ -40,7 +40,7 @@ var arraySum = function(array) { }; // 4. Check if a number is even. -var isEven = function (n, i = n) { +var isEven = function(n, i = n) { if (i - 2 === 0) { return boolean = true; } else if (i - 2 > 0) { @@ -55,12 +55,13 @@ var isEven = function (n, i = n) { 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) { +var sumBelow = function(n, sum = 0, i = n) { if (n === 0) { return sum; } if (n > 0) { i = n - 1; @@ -76,11 +77,24 @@ var sumBelow = function (n, sum = 0, i = n) { 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) { + if (y !== x - 1) { + x--; + arr.push(x); + return range(x, y, arr); + } + return arr; + } + if (x !== y - 1) { + x++; + arr.push(x); + return range(x, y, arr); + } + return arr; }; @@ -89,22 +103,58 @@ 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, 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(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 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 6/8] 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 c6bc725f863f932a750ba09433899bde98fbc7c4 Mon Sep 17 00:00:00 2001 From: alexo12 Date: Tue, 25 Sep 2018 15:29:25 -0500 Subject: [PATCH 7/8] fibonacci --- src/recursion.js | 82 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 155f876e8..b23427189 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -154,6 +154,9 @@ var palindrome = function(string, i = 0) { i++; return palindrome(string, i); } + else { + return false; + } return false; }; @@ -167,7 +170,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, total = 0, i = y) { +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,total,i); +} +if(y < 0) { + total += x; + i++; + return multiply(x,y,total, i); +} }; // 13. Write a function that divides two numbers without using the / operator or @@ -188,33 +204,73 @@ 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 === str2.length && i === str1.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(str, array = [], i = 0){ + if(i === str.length) {return array;} + if(str.charAt(i)) { + array.push(str.charAt(i)); + i++; + return createArray(str, array, i); + } + }; // 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, array = [], i = 0) { + if(i === length) {return array;} + array.push(value); + i++; + return buildList(value,length, array, i); + + }; // 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. @@ -249,7 +305,15 @@ var fibonacci = function(n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function(n) { +var nthFibo = function(n, i = 0, array = [], total = 0,) { + if(array[i] === n) {return n;} + if(i > n) {return null;} + + array[i] = total += n; + array.push(n); + i++; + return nthFibo(n,i,array, total); + }; // 26. Given an array of words, return a new array containing each word capitalized. From c8646a7c7828c59b8c1d8005bdea311e69204cfa Mon Sep 17 00:00:00 2001 From: alexo12 Date: Fri, 28 Sep 2018 15:55:23 -0500 Subject: [PATCH 8/8] almost done palindrome --- src/recursion.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index d6b5ad3b2..5034c841b 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -134,15 +134,22 @@ var reverse = function (string) { */ // 10. Write a function that determines if a string is a palindrome. -var palindrome = function (string, i = string.length) { - if( string = string.substr(1) + string.charAt(0)) {return true;} - else if(!reverse(string)) { - return reverse(string.substr(1, string.length - 1)); - } else { - return false; +var palindrome = function (string) { + /* if (string) { return true; } + else {return false;} + string = string.substr(1) + string.charAt(0); + return palindrome(string); + /* + /* --------------------------------------------------------*/ + if (!reverse(string)) { return false;} + else { + return true; } - return palindrome(string,i); + return palindrome(string); + + + };