diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 000000000..783ebf08d Binary files /dev/null and b/.DS_Store differ diff --git a/src/recursion.js b/src/recursion.js index 12ca0a3d7..543dcf995 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -1,60 +1,160 @@ // 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) { -}; - +var factorial = function(n, total, i = n) { + if(total === undefined){total = 1} + if(i < 0){ + return null; + }else if(i === 0){ + return total + } + total *= i; + i--; + 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) { +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) { + if(n === 0){ + return true; + }else if(n === 1){ + return false; + } + if(n < 0){ + n = n + 2; + }else if(n > 0){ + 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,i = n) { + if(i === 0 ){ + return total; + } + if(n > 0){ + i--; + total = total + i; + }else if(n < 0){ + i++; + total = total + i; + } + + + return sumBelow(n,total,i); +}; + // 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, array = [], i = x) { + if (x === y || x === y + 1) { + return array; + } + if (x > y) { + i--; + } else if (x < y) { + i++; + } + if (i === y) { + return array; + } + array.push(i); + + + return range(x, y, array, i); +}; + // 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) { -}; - +var exponent = function(base, exp, i = exp, total = 1) { + if (exp === 0) { + return 1; + } else if (exp === 1) { + return base; + } + if (i === 0){ + return total; + } + if (exp < 0) { + total /= base; + i++; + } else if (exp > 0) { + total *= base; + i--; + } + + return exponent(base,exp,i, 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, total = n) { + if(n === 1){ + return true + }else if(n === 0){ + return false + } + if(total === 2){ + return true + }else if(total < 2){ + return false + } + total = total/2 + return powerOfTwo(n, total) + +}; + // 9. Write a function that accepts a string a reverses it. -var reverse = function(string) { -}; - +var reverse = function(string, i = string.length-1, finalString="") { + if(i < 0){ + return finalString + } + finalString = finalString.concat(string.charAt(i)) + i-- + return reverse(string, i, finalString) +}; + // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string) { -}; - +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 @@ -62,17 +162,28 @@ var palindrome = function(string) { // 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) { -}; - +var multiply = function(x, y, i = y, total = 0) { + if(i === 0){ + return total + } + if( y < 0){ + total -= x + i++ + } else 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 @@ -80,60 +191,124 @@ var divide = function(x, y) { // 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) { -}; - +var compareStr = function(str1, str2, i = 0, truth = 0) { + + if(i === str1.length && i === str2.length){ + + if(truth === str1.length && truth === str2.length){ + return true + }else{ + return false + } + } + if(str1.length === str2.length){ + if(str1.charAt(i) === str2.charAt(i)){ + truth++ + } + }else{ + return false + } + i++ + return compareStr(str1, str2, i, truth) +}; + // 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, final = []){ + // if (final[0] == str[0]) { + // final.push(i); + // i++; + // } + // if (str == final) { + // return final; + // } + + //String.prototype.split() cant use + if(i === str.length){ + return final; + } + final.push(str.charAt(i)); + i++; + return createArray(str, i, final); +}; + // 17. Reverse the order of an array -var reverseArr = function (array) { -}; - +var reverseArr = function (array, i = array.length - 1, finalArr = []) { + if (i < 0) { + return finalArr; + } + finalArr.push(array[i]); + i--; + return reverseArr(array, i, finalArr); +}; + // 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 = length, array = []) { + if (i === 0) { + 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, number = 0) { + /* +make an if statement says if value is inside the array => number++ +*/ + //Base Case => i equals array.length - 1 + if(i === array.length){ + return number + } + if(array[i] === value){ + number++ + } + i++ + return countOccurrence(array, value, i, number) +}; + // 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, arr = []) { + if (i === array.length) { + return arr; + } + arr.push(callback(array[i])); + i++ + return rMap(array, callback, i, arr); +}; + // 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..... @@ -141,26 +316,51 @@ var replaceKeysInObj = function(obj, key, newKey) { // 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) { -}; - +var nthFibo = function(n, i = 0, number = 0, array = [0, 1, 1]) { + if (n < 0) { + return null; + } + if (array.length-1 >= n) { + return array[n]; + } else { + array.push((array[array.length - 1] + array[array.length - 2])); + } + return nthFibo(n, i, number, 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, i = 0, array = []) { + if (input.length === array.length) { + return array; + } + array.push(input[i].toUpperCase()); + i++ + return capitalizeWords(input, i, array); +}; + // 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, i = 0, arr = [], firstChar,string) { + if (array.length === arr.length) { + return arr; + } + string = array[i]; + firstChar = string.slice(0,1); + firstChar = firstChar.toUpperCase(); + string = string.replace(string.charAt(0), firstChar) + arr.push(string); + i++; + return capitalizeFirst(array, i, arr, string); +}; + // 28. Return the sum of all even numbers in an object containing nested objects. // var obj1 = { // a: 2, @@ -172,75 +372,127 @@ var capitalizeFirst = function(array) { // 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) { -}; - +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) { -}; - +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) { -}; - +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) { -}; - +var alternateSign = function(array, arr = [], i = 0) { + if(i === array.length){ + return arr + } + + + + if(i%2 != 0){ + if(array[i] < 0){ + arr.push(array[i] * 1) + }else if(array[i] > 0){ + arr.push(array[i] * -1) + } + }else if(i%2 === 0){ + if(array[i] < 0){ + arr.push(array[i] * -1) + }else if(array[i] > 0){ + arr.push(array[i] * 1) + } + } + i++ + return alternateSign(array, arr, i) +}; + // 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 = { @@ -269,5 +521,5 @@ if ((typeof process !== 'undefined') && alternateSign, }; } - + //----------------------------------- \ No newline at end of file