From ad9c286ac0091610e2f155df44b6a4235a39dfb6 Mon Sep 17 00:00:00 2001 From: anigonzales Date: Thu, 20 Sep 2018 13:55:25 -0700 Subject: [PATCH 1/8] working on sum --- src/recursion.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/recursion.js b/src/recursion.js index 12ca0a3d7..7ef64158d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -4,12 +4,33 @@ // 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, i = n) { + // base case + if(i < 0){ + return null; + }else if(i === 0){ + return total + } + + //changes value of total + total *= i; + + // brings i down 1 value (if n = 5 and i = n, then i now = 4) + i--; + + //starting with n = 5, then this /v is factorial(4, 20, 4) + return factorial(n, total, i); }; // 2. Compute the sum of an array of integers. // Example: sum([1, 2, 3, 4, 5, 6]); // 21 var sum = function(array) { + return array[0]; + // if(_.contains(array, values < 0)){ + // return; + // } + + }; // 3. Sum all numbers in an array containing nested arrays. @@ -19,6 +40,8 @@ var arraySum = function(array) { // 4. Check if a number is even. var isEven = function(n) { + return true; + }; // 5. Sum all integers below a given integer. From b056097150fdd8addbde317c24f7cfce796a48bc Mon Sep 17 00:00:00 2001 From: anigonzales Date: Fri, 21 Sep 2018 13:59:49 -0700 Subject: [PATCH 2/8] on range --- src/recursion.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 7ef64158d..e6fa1c0db 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -24,30 +24,51 @@ var factorial = function(n, total = 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) { - return array[0]; - // if(_.contains(array, values < 0)){ - // return; - // } - - +var sum = function(array, total = 0, i = 0) { + if(!array.length){ + return 0 + } + return array[0] + sum(array.slice(1)); }; // 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) { - return true; + if(n === 0){ + return true; + } + if(n === -1){ + return false; + } + if(n < 0){ + n *= -1 + } + n = n -2 + return isEven(n) }; // 5. Sum all integers below a given integer. // sumBelow(10); // 45 // sumBelow(7); // 21 -var sumBelow = function(n) { +var sumBelow = function(n, total = 0) { + if(n === 0){ + return total; + } + if (n < 0){ + total += n + 1; + n++; + } + if(n > 0){ + total += n - 1; + n--; + } + return sumBelow(n, total); }; // 6. Get the integers in range (x, y). From c6b4258fac15194027ed97d8c4d9ffe781f65c1e Mon Sep 17 00:00:00 2001 From: anigonzales Date: Mon, 24 Sep 2018 13:55:43 -0700 Subject: [PATCH 3/8] need to accept neg integers for exponent --- src/recursion.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/recursion.js b/src/recursion.js index e6fa1c0db..afe157ec0 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -73,7 +73,25 @@ var sumBelow = function(n, total = 0) { // 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 || arr[arr.length-1] === y - 1 || arr[arr.length-1] === y + 1){ + return arr; + } + if(x + 1 === y || x - 1 === y){ + return arr; + } + if ( x < y){ + arr.push(x + 1); + x++; + // one less + } + if (x > y){ + arr.push(x - 1); + x--; + // one more + } + return range(x,y, arr); }; // 7. Compute the exponent of a number. @@ -82,6 +100,23 @@ var range = function(x, y) { // 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) { + // if(exp === 1){ + // return base; + // } + // if(exp === 0){ + // return 1; + // } + if(exp === 0){ + return 1; + } + if(exp < 0){ + return 0; + } + if(exp === 1){ + return base; + } else { + return base * exponent(base, exp - 1); + } }; // 8. Determine if a number is a power of two. @@ -89,6 +124,8 @@ var exponent = function(base, exp) { // powerOfTwo(16); // true // powerOfTwo(10); // false var powerOfTwo = function(n) { + //2^n + }; // 9. Write a function that accepts a string a reverses it. From b50ee61d1c87361d86860a51890062d94d53fd66 Mon Sep 17 00:00:00 2001 From: anigonzales Date: Tue, 25 Sep 2018 11:55:03 -0700 Subject: [PATCH 4/8] finished power of two --- src/recursion.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index afe157ec0..c04db0264 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -110,7 +110,7 @@ var exponent = function(base, exp) { return 1; } if(exp < 0){ - return 0; + return exponent(base, exp + 1) } if(exp === 1){ return base; @@ -125,11 +125,23 @@ var exponent = function(base, exp) { // powerOfTwo(10); // false var powerOfTwo = function(n) { //2^n - + if(n <= 0){ + return false; + }else if(n === 1 || n === 0) { + return true; + }else if(n % 2 === 1){ + return false; + }else{ + return powerOfTwo(n/2); + } }; // 9. Write a function that accepts a string a reverses it. var reverse = function(string) { + if(!string.length){ + return string; + } + return string.split("").reverse(string).join(""); }; // 10. Write a function that determines if a string is a palindrome. From 425829dc615781602a6990f329804e31fc6d9d91 Mon Sep 17 00:00:00 2001 From: anigonzales Date: Tue, 25 Sep 2018 13:07:31 -0700 Subject: [PATCH 5/8] on palindrome --- src/recursion.js | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index c04db0264..dd1a2515c 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -77,20 +77,20 @@ var range = function(x ,y, arr = []) { if(x === y || arr[arr.length-1] === y - 1 || arr[arr.length-1] === y + 1){ return arr; - } - if(x + 1 === y || x - 1 === y){ + } + if(x + 1 === y || x - 1 === y){ return arr; - } - if ( x < y){ + } + if ( x < y){ arr.push(x + 1); x++; // one less - } - if (x > y){ + } + if (x > y){ arr.push(x - 1); x--; // one more - } + } return range(x,y, arr); }; @@ -110,12 +110,12 @@ var exponent = function(base, exp) { return 1; } if(exp < 0){ - return exponent(base, exp + 1) + return 1 / exponent(base, -exp) } if(exp === 1){ return base; } else { - return base * exponent(base, exp - 1); + return base *= exponent(base, exp - 1); } }; @@ -137,15 +137,34 @@ var powerOfTwo = function(n) { }; // 9. Write a function that accepts a string a reverses it. -var reverse = function(string) { - if(!string.length){ +var reverse = function (startString, string = "", newString = '', arr = []) { + if (startString.length === 0){ return string; + } else if(startString.length > 0){ + for (let i = startString.length - 1; i >= 0; i--) { + arr.push(startString[i]); + newString += arr[0]; + arr.shift(); + } } - return string.split("").reverse(string).join(""); + return (reverse(string, newString ,arr)) }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string) { +var palindrome = function(string, string2= string) { + if(string.length === 0){ + return false; + } + + if(string[0] === string2[string2.length-1]){ + string.replace(string.charAt(0), ""); + string2.replace(string2.charAt(string2.length-1), ""); + return true; + } + + + + return palindrome(string, string2); }; // 11. Write a function that returns the remainder of x divided by y without using the From 634849c60e4dbf2ee9900495a4743aed1041b630 Mon Sep 17 00:00:00 2001 From: anigonzales Date: Tue, 25 Sep 2018 13:59:15 -0700 Subject: [PATCH 6/8] merging --- src/recursion.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/recursion.js b/src/recursion.js index dd1a2515c..81408a15b 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -178,6 +178,7 @@ 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) { + }; // 13. Write a function that divides two numbers without using the / operator or From fde1ae59b402422004ef0eccc82f523e9b470589 Mon Sep 17 00:00:00 2001 From: anigonzales Date: Fri, 28 Sep 2018 14:00:42 -0700 Subject: [PATCH 7/8] finish recursion --- src/recursion.js | 167 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 142 insertions(+), 25 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 81408a15b..6d9c1b67d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -151,22 +151,19 @@ var reverse = function (startString, string = "", newString = '', arr = []) { }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string, string2= string) { - if(string.length === 0){ - return false; - } - - if(string[0] === string2[string2.length-1]){ - string.replace(string.charAt(0), ""); - string2.replace(string2.charAt(string2.length-1), ""); +var palindrome = function(string) { + if (string != string.split("").reverse().join("")){ return true; + }else if(string.length){ + if (string[0] == string.split("").reverse().join("")[0]) { + return palindrome(string.substring(1)); + }else{ + return false; + } } - - - - return palindrome(string, string2); }; + // 11. Write a function that returns the remainder of x divided by y without using the // modulo (%) operator. // modulo(5,2) // 1 @@ -178,7 +175,13 @@ 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) { - + if (x === 0 || y === 0) { + return 0; + } else if (y < 0) { + return -x + multiply(x, y + 1); + } else { + return x + multiply(x, y - 1); + } }; // 13. Write a function that divides two numbers without using the / operator or @@ -200,32 +203,80 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { + if(str1.length === 0 && str2.length === 0){ + return true; + } + if(str1.charAt(0) === str2.charAt(0)){ + return compareStr(str1.slice(1), str2.slice(1)); + } + + + if(str1.charAt(0) !== str2.charAt(0)){ + return false; + } }; // 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, i = 0, arr = []){ + if(i >= str.length){ + return arr; + } + else{ + arr.push(str[i]); + } + i++; + return createArray(str, i, arr); }; // 17. Reverse the order of an array -var reverseArr = function (array) { +var reverseArr = function (array, array2 = [], num = 1) { + if (num >= array.length + 1){ + return array2; + }else{ + array2.push(array[array.length - num]); + } + num++ + return reverseArr(array, array2, num) }; // 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; + }else { + array.push(value); + }i++ + return buildList(value, length, array, i); }; -// 19. Count the occurence of a value inside a list. +// 19. Count the occurrence 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, sum = 0) { + if(i >= array.length){ + return sum; + } + if(array[i] === value){ + sum += 1; + } + + i++; + return countOccurrence(array, value, i, sum); }; // 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, newArray = []) { + if(i >= array.length){ + return newArray; + }else{ + newArray.push(callback(array[i])); + } + i++; + return rMap(array, callback, i, newArray) }; // 21. Write a function that counts the number of times a key occurs in an object. @@ -261,17 +312,42 @@ var fibonacci = function(n) { // nthFibo(7); // 13 // nthFibo(3); // 2 var nthFibo = function(n) { + if (n === 0) { + return 0; + } + if (n === 1) { + return 1; + } + if(n < 0){ + return null; + } + + return nthFibo(n - 1) + nthFibo(n - 2); }; // 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, i = 0, arr = []) { + if(i >= input.length){ + return arr; + }else{ + arr.push(input[i].toUpperCase()); + } + i++; + return capitalizeWords(input,i,arr); }; // 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(input, i = 0, arr = []) { + if(i >= input.length){ + return arr; + }else{ + arr.push(input[i][0].toUpperCase() + input[i].substring(1)); + } + i++; + return capitalizeFirst(input,i,arr); }; // 28. Return the sum of all even numbers in an object containing nested objects. @@ -293,7 +369,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(str, obj, i = 0) { + if(str.length < i){ + return obj; + } + if(str.length > i){ + if(obj[i] === false){ + // if i is not in obj then add it as a key and a value 1 + obj.i = "1" + + }else{ + // if i is in obj then add it as a value of +1 + + + } + } + return letterTally(str, obj, i); }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -301,7 +392,18 @@ 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, i = 0, arr = []) { + if(i >= list.length){ + return arr; + } + if(list[i] - 1 === list[i] || list[i] + 1 === list[i]){ + list.slice(list[i]); + } + if(arr.indexOf(list[i]) === -1){ + arr.push(list[i]); + } + i++; + return compress(list, i, arr); }; // 32. Augment every element in a list with a new value where each element is an array @@ -320,8 +422,23 @@ 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 = 0) { + if (i >= array.length){ + array[0] = Math.abs(array[0]); + return array; + }else if(i % 2 !== 0 && array[i] >= 0){ + array[i] = array[i]*-1; + }else if (i % 2 !== 0 && array[i] < 0){ + array[i] = array[i]; + }else if (i % 2 === 0 && array[i] < 0){ + array[i] = array[i] * -1; + }else if (i % 2 === 0 && array[i] >= 0) { + array[i] = array[i]; + } + + i++; + return alternateSign(array, i); +} // 35. Given a string, return a string with digits converted to their word equivalent. // Assume all numbers are single digits (less than 10). From 6ba4b9644160ffd55fce8adf3cdbea23c0cec49e Mon Sep 17 00:00:00 2001 From: Half Day Date: Fri, 16 Nov 2018 14:32:15 -0600 Subject: [PATCH 8/8] finished --- src/recursion.js | 258 +++++++++++++++++++++++++---------------------- 1 file changed, 135 insertions(+), 123 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 6d9c1b67d..993eff36d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,14 +1,14 @@ -// Solve all of the following prompts using recursion. +/// 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, total = 1, i = n) { +var factorial = function (n, total = 1, i = n) { // base case - if(i < 0){ + if (i < 0) { return null; - }else if(i === 0){ + } else if (i === 0) { return total } @@ -24,8 +24,8 @@ var factorial = function(n, total = 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 = 0) { - if(!array.length){ +var sum = function (array, total = 0, i = 0) { + if (!array.length) { return 0 } return array[0] + sum(array.slice(1)); @@ -33,38 +33,38 @@ var sum = function(array, total = 0, i = 0) { // 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) { - if(n === 0){ +var isEven = function (n) { + if (n === 0) { return true; } - if(n === -1){ + if (n === -1) { return false; } - if(n < 0){ + if (n < 0) { n *= -1 } - n = n -2 + n = n - 2 return isEven(n) }; // 5. Sum all integers below a given integer. // sumBelow(10); // 45 // sumBelow(7); // 21 -var sumBelow = function(n, total = 0) { - if(n === 0){ +var sumBelow = function (n, total = 0) { + if (n === 0) { return total; } - if (n < 0){ + if (n < 0) { total += n + 1; n++; } - if(n > 0){ + if (n > 0) { total += n - 1; n--; } @@ -73,25 +73,25 @@ var sumBelow = function(n, total = 0) { // 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 || arr[arr.length-1] === y - 1 || arr[arr.length-1] === y + 1){ +var range = function (x, y, arr = []) { + + if (x === y || arr[arr.length - 1] === y - 1 || arr[arr.length - 1] === y + 1) { return arr; - } - if(x + 1 === y || x - 1 === y){ + } + if (x + 1 === y || x - 1 === y) { return arr; } - if ( x < y){ + if (x < y) { arr.push(x + 1); x++; // one less } - if (x > y){ + if (x > y) { arr.push(x - 1); x--; // one more } - return range(x,y, arr); + return range(x, y, arr); }; // 7. Compute the exponent of a number. @@ -99,20 +99,20 @@ 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) { // if(exp === 1){ // return base; // } // if(exp === 0){ // return 1; // } - if(exp === 0){ + if (exp === 0) { return 1; } - if(exp < 0){ + if (exp < 0) { return 1 / exponent(base, -exp) } - if(exp === 1){ + if (exp === 1) { return base; } else { return base *= exponent(base, exp - 1); @@ -123,58 +123,60 @@ var exponent = function(base, exp) { // powerOfTwo(1); // true // powerOfTwo(16); // true // powerOfTwo(10); // false -var powerOfTwo = function(n) { +var powerOfTwo = function (n) { //2^n - if(n <= 0){ + if (n <= 0) { return false; - }else if(n === 1 || n === 0) { + } else if (n === 1 || n === 0) { return true; - }else if(n % 2 === 1){ + } else if (n % 2 === 1) { return false; - }else{ - return powerOfTwo(n/2); + } else { + return powerOfTwo(n / 2); } }; // 9. Write a function that accepts a string a reverses it. var reverse = function (startString, string = "", newString = '', arr = []) { - if (startString.length === 0){ + if (startString.length === 0) { return string; - } else if(startString.length > 0){ + } else if (startString.length > 0) { for (let i = startString.length - 1; i >= 0; i--) { arr.push(startString[i]); newString += arr[0]; arr.shift(); } } - return (reverse(string, newString ,arr)) + return (reverse(string, newString, arr)); }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string) { - if (string != string.split("").reverse().join("")){ - return true; - }else if(string.length){ - if (string[0] == string.split("").reverse().join("")[0]) { - return palindrome(string.substring(1)); - }else{ - return false; - } +var palindrome = function (string) { + if (string.length <= 1) { return true; } + if (string[0].toLowerCase() !== string[string.length - 1].toLowerCase()) { + return false; } + 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. // 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) { if (x === 0 || y === 0) { return 0; } else if (y < 0) { @@ -186,7 +188,7 @@ 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 @@ -194,7 +196,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 @@ -202,27 +204,27 @@ var gcd = function(x, y) { // compareStr('house', 'houses') // false // compareStr('', '') // true // compareStr('tomato', 'tomato') // true -var compareStr = function(str1, str2) { - if(str1.length === 0 && str2.length === 0){ +var compareStr = function (str1, str2) { + if (str1.length === 0 && str2.length === 0) { return true; } - if(str1.charAt(0) === str2.charAt(0)){ + if (str1.charAt(0) === str2.charAt(0)) { return compareStr(str1.slice(1), str2.slice(1)); } - - - if(str1.charAt(0) !== str2.charAt(0)){ + + + if (str1.charAt(0) !== str2.charAt(0)) { return false; } }; // 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, i = 0, arr = []){ - if(i >= str.length){ +var createArray = function (str, i = 0, arr = []) { + if (i >= str.length) { return arr; } - else{ + else { arr.push(str[i]); } i++; @@ -231,9 +233,9 @@ var createArray = function(str, i = 0, arr = []){ // 17. Reverse the order of an array var reverseArr = function (array, array2 = [], num = 1) { - if (num >= array.length + 1){ + if (num >= array.length + 1) { return array2; - }else{ + } else { array2.push(array[array.length - num]); } num++ @@ -243,36 +245,36 @@ var reverseArr = function (array, array2 = [], num = 1) { // 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, array = [], i = 0) { - if(i >= length){ +var buildList = function (value, length, array = [], i = 0) { + if (i >= length) { return array; - }else { + } else { array.push(value); - }i++ + } i++ return buildList(value, length, array, i); }; // 19. Count the occurrence 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, sum = 0) { - if(i >= array.length){ - return sum; +var countOccurrence = function (array, value, i = 0, sum = 0) { + if (i >= array.length) { + return sum; } - if(array[i] === value){ + if (array[i] === value) { sum += 1; } - + i++; return countOccurrence(array, value, i, sum); }; // 20. Write a recursive version of map. // rMap([1,2,3], timesTwo); // [2,4,6] -var rMap = function(array, callback, i = 0, newArray = []) { - if(i >= array.length){ +var rMap = function (array, callback, i = 0, newArray = []) { + if (i >= array.length) { return newArray; - }else{ + } else { newArray.push(callback(array[i])); } i++; @@ -283,19 +285,19 @@ var rMap = function(array, callback, i = 0, newArray = []) { // 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 @@ -303,7 +305,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. @@ -311,14 +313,14 @@ var fibonacci = function(n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function(n) { +var nthFibo = function (n) { if (n === 0) { return 0; } if (n === 1) { return 1; } - if(n < 0){ + if (n < 0) { return null; } @@ -328,26 +330,26 @@ 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, i = 0, arr = []) { - if(i >= input.length){ +var capitalizeWords = function (input, i = 0, arr = []) { + if (i >= input.length) { return arr; - }else{ + } else { arr.push(input[i].toUpperCase()); } i++; - return capitalizeWords(input,i,arr); + return capitalizeWords(input, i, arr); }; // 27. Given an array of strings, capitalize the first letter of each index. // capitalizeFirst(['car', 'poop', 'banana']); // ['Car', 'Poop', 'Banana'] -var capitalizeFirst = function(input, i = 0, arr = []) { - if(i >= input.length){ +var capitalizeFirst = function (input, i = 0, arr = []) { + if (i >= input.length) { return arr; - }else{ + } else { arr.push(input[i][0].toUpperCase() + input[i].substring(1)); } i++; - return capitalizeFirst(input,i,arr); + return capitalizeFirst(input, i, arr); }; // 28. Return the sum of all even numbers in an object containing nested objects. @@ -359,32 +361,30 @@ var capitalizeFirst = function(input, i = 0, arr = []) { // 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, i = 0) { - if(str.length < i){ +var letterTally = function (str, obj = {}) { + if (str.length === 0) { return obj; } - if(str.length > i){ - if(obj[i] === false){ - // if i is not in obj then add it as a key and a value 1 - obj.i = "1" - - }else{ - // if i is in obj then add it as a value of +1 + if (obj.hasOwnProperty(str[0])) { + obj[str[0]] += 1; + return letterTally(str.slice(1), obj); - } + } else { + obj[str[0]] = 1; } - return letterTally(str, obj, i); + return letterTally(str.slice(1), obj); + }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -392,47 +392,59 @@ var letterTally = function(str, obj, i = 0) { // 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, i = 0, arr = []) { - if(i >= list.length){ +var compress = function (list, arr = []) { + if (list.length === 0) { return arr; } - if(list[i] - 1 === list[i] || list[i] + 1 === list[i]){ - list.slice(list[i]); - } - if(arr.indexOf(list[i]) === -1){ - arr.push(list[i]); + + if (list[0] !== list[1]) { + arr.push(list[0]); + return compress(list.slice(1), arr); } - i++; - return compress(list, i, arr); + + return compress(list.slice(1), arr); }; // 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, newArr = []) { + if (array.length === 0) { return newArr } + + if (array[0] !== 0) { + newArr.push(array[0]); + return minimizeZeroes(array.slice(1), newArr); + } + + if (array[0] !== array[1]) { + newArr.push(array[0]); + return minimizeZeroes(array.slice(1), newArr); + } + return minimizeZeroes(array.slice(1), newArr); }; + // 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 = 0) { - if (i >= array.length){ - array[0] = Math.abs(array[0]); +var alternateSign = function (array, i = 0) { + if (i >= array.length) { + array[0] = Math.abs(array[0]); return array; - }else if(i % 2 !== 0 && array[i] >= 0){ - array[i] = array[i]*-1; - }else if (i % 2 !== 0 && array[i] < 0){ + } else if (i % 2 !== 0 && array[i] >= 0) { + array[i] = array[i] * -1; + } else if (i % 2 !== 0 && array[i] < 0) { array[i] = array[i]; - }else if (i % 2 === 0 && array[i] < 0){ + } else if (i % 2 === 0 && array[i] < 0) { array[i] = array[i] * -1; - }else if (i % 2 === 0 && array[i] >= 0) { + } else if (i % 2 === 0 && array[i] >= 0) { array[i] = array[i]; } @@ -443,26 +455,26 @@ var alternateSign = function(array, i = 0) { // 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) { };