From ddf87a8d8f5af96d57ebcd45c8bd65591ac6096f Mon Sep 17 00:00:00 2001 From: Christopher Boyd Date: Wed, 5 Aug 2020 10:07:26 -0500 Subject: [PATCH 1/9] Finished Factorial and Integer Sum tests. --- src/recursion.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index 3b622b4af..b37eff1b1 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -5,16 +5,43 @@ // Example: 5! = 5 x 4 x 3 x 2 x 1 = 120 // factorial(5); // 120 var factorial = function(n) { + + if (Math.sign(n) === -1){ + return null + } + + if (n === 0){ + return 1 + } + + return n * factorial(n - 1) }; // 2. Compute the sum of an array of integers. // Example: sum([1, 2, 3, 4, 5, 6]); // 21 var sum = function(array) { + + 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) { + + if (!array.length){ + return 0 + } + + if (Array.isArray(array[0])){ + arraySum(array[0]) // continue logic here + } + + return array[0]+arraySum(array.slice(1)) + }; // 4. Check if a number is even. From 76cf5b57c102a156e436dbedd6289a02753a96b6 Mon Sep 17 00:00:00 2001 From: Justin Nguyen Date: Wed, 5 Aug 2020 11:06:52 -0500 Subject: [PATCH 2/9] On Integer Raange --- spec/part1.js | 2 +- src/recursion.js | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/spec/part1.js b/spec/part1.js index 713e94a4e..33453395d 100755 --- a/spec/part1.js +++ b/spec/part1.js @@ -84,7 +84,7 @@ - xdescribe('3. Sum Integers in Array', function() { + describe('3. Sum Integers in Array', function() { it('should return a number', function() { expect(typeof(arraySum([[1],[2,3],[[4]],5,6]))).to.eql('number'); diff --git a/src/recursion.js b/src/recursion.js index b37eff1b1..3bef1689d 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -37,21 +37,52 @@ var arraySum = function(array) { } if (Array.isArray(array[0])){ - arraySum(array[0]) // continue logic here + var sum = arraySum(array[0]) // continue logic here + } + else { + var sum = array[0] } - return array[0]+arraySum(array.slice(1)) + return sum + arraySum(array.slice(1)) }; // 4. Check if a number is even. var isEven = function(n) { + if (n === 0) { + return true + } + else if (n === 1) { + return false + } + if (Math.sign(n) === -1) { + return isEven(n + 2) + } + else { + return isEven(n - 2) + } }; // 5. Sum all integers below a given integer. // sumBelow(10); // 45 // sumBelow(7); // 21 var sumBelow = function(n) { + if (n === 0) { + return 0 + } + if (Math.sign(n) === -1){ + var currentN = n + 1} + else{ + var currentN = n - 1} + if (currentN === 0){ + return 0 + } + if (Math.sign(currentN) === -1) { + return currentN + sumBelow(currentN) + } + else { + return currentN + sumBelow(currentN) + } }; // 6. Get the integers in range (x, y). From 179166a177bf6c7684d46edef488d109226a7702 Mon Sep 17 00:00:00 2001 From: Christopher Boyd Date: Wed, 5 Aug 2020 12:29:31 -0500 Subject: [PATCH 3/9] Finished range, exponent, power of two, reverse, and palindrome test solutions. --- spec/part1.js | 2 +- src/recursion.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/spec/part1.js b/spec/part1.js index 33453395d..5e4b0b401 100755 --- a/spec/part1.js +++ b/spec/part1.js @@ -402,7 +402,7 @@ - xdescribe('11. Modulo', function() { + describe('11. Modulo', function() { it('should return a number', function() { expect(typeof(modulo(5,2))).to.equal('number'); diff --git a/src/recursion.js b/src/recursion.js index 3bef1689d..1144f0971 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -88,6 +88,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) { + //debugger; + + if (x === y || x-1 === y || x+1 === y || y === undefined){ + return[] + } + + if(x < y){ + var nextX = x+1 + } + if(x > y){ + var nextX = x-1 + } + + return [nextX].concat(range(nextX, y)) + }; // 7. Compute the exponent of a number. @@ -96,6 +111,19 @@ 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 === 0){ + return 1 + } + if (exp === 1){ + return base + } + if (exp > 0){ + return base * exponent(base, exp-1) + } + if (exp < 0){ + return 1 / exponent(base, (-1*exp)) + } }; // 8. Determine if a number is a power of two. @@ -103,14 +131,42 @@ var exponent = function(base, exp) { // powerOfTwo(16); // true // powerOfTwo(10); // false var powerOfTwo = function(n) { + + if(n === 1){ + return true + }else if(n % 2 !== 0 || n === 0){ + return false + } + + return powerOfTwo(n/2) }; -// 9. Write a function that accepts a string a reverses it. +// 9. Write a function that accepts a string and reverses it. var reverse = function(string) { + +if (Array.isArray(string)){ + var reversedText = string.reverse() + return reversedText.join("") +}else{ + var text = string + var textArr = text.split("") + return reverse(textArr) +} + }; // 10. Write a function that determines if a string is a palindrome. var palindrome = function(string) { + + if (Array.isArray(string)){ + var reversedText = string.reverse() + return reversedText.join("") + }else{ + var correctString = string.toLowerCase() + var correctString1 = correctString.replace(/\s+/g, ''); + return (correctString1 === palindrome(correctString1.split(""))) + } + }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -119,6 +175,13 @@ var palindrome = function(string) { // modulo(17,5) // 2 // modulo(22,6) // 4 var modulo = function(x, y) { +//debugger; +if (y > x){ + return x +}else if(y < x){ + return modulo(x-y, y) +} + }; // 12. Write a function that multiplies two numbers without using the * operator or From c92e2faf4c3163828ea2a5e6fb27b218142732eb Mon Sep 17 00:00:00 2001 From: Christopher Boyd Date: Wed, 5 Aug 2020 14:18:55 -0500 Subject: [PATCH 4/9] Finished Modulo test solution. --- spec/part1.js | 4 ++-- src/recursion.js | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/spec/part1.js b/spec/part1.js index 5e4b0b401..93097eca5 100755 --- a/spec/part1.js +++ b/spec/part1.js @@ -476,7 +476,7 @@ - xdescribe('13. Divide', function() { + describe('13. Divide', function() { it('should return a number', function() { expect(typeof(divide(5,2))).to.equal('number'); @@ -512,7 +512,7 @@ - xdescribe('14. Greatest Common Divisor', function() { + describe('14. Greatest Common Divisor', function() { it('should return a number', function() { expect(typeof(gcd(4,36))).to.equal('number'); diff --git a/src/recursion.js b/src/recursion.js index 1144f0971..5e796a122 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -175,12 +175,47 @@ var palindrome = function(string) { // modulo(17,5) // 2 // modulo(22,6) // 4 var modulo = function(x, y) { -//debugger; -if (y > x){ + if( x === 0 && y === 0){ + return NaN + } + + if (x >= 0 && y >= 0){ + + if (y > x){ + return x + }else if(y <= x){ + return modulo(x-y, y) + } + + } + + if (x < 0 && y < 0){ + + if (y < x){ + return x + }else if(y >= x){ + return modulo(x-y, y) + } + + } + + if (x < 0 && y > 0){ + + if (y < x){ + return x + }else if(y >= x){ + + var negativeXModulo = modulo(y+x, y) + + + if(x < y && negativeXModulo > 0){ return x -}else if(y < x){ - return modulo(x-y, y) -} + }else{ + return negativeXModulo + } + + } + } }; From 6c395118d0bae998d1e950ac598f46484ffb963c Mon Sep 17 00:00:00 2001 From: Christopher Boyd Date: Wed, 5 Aug 2020 14:43:10 -0500 Subject: [PATCH 5/9] Work done on multiply test. --- src/recursion.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 5e796a122..94a76c638 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -178,50 +178,50 @@ var modulo = function(x, y) { if( x === 0 && y === 0){ return NaN } - if (x >= 0 && y >= 0){ - if (y > x){ return x }else if(y <= x){ return modulo(x-y, y) } - } - if (x < 0 && y < 0){ - if (y < x){ return x }else if(y >= x){ return modulo(x-y, y) } - } - if (x < 0 && y > 0){ - if (y < x){ return x }else if(y >= x){ - var negativeXModulo = modulo(y+x, y) - - if(x < y && negativeXModulo > 0){ return x }else{ return negativeXModulo } - } } - }; // 12. Write a function that multiplies two numbers without using the * operator or // JavaScript's Math object. var multiply = function(x, y) { + +if (y === 1){ + return x +} + +if (y > 0){ + return x+multiply(x, y-1) +}else if (y <= 0 && x < 0){ + return x-multiply(x, y+1) // problem with -275, -502; returns 0. +}else if (y < 0){ + return x+multiply(x, y+1) +} + }; // 13. Write a function that divides two numbers without using the / operator or From 667b821bbd003cca195b35712fa09122582de484 Mon Sep 17 00:00:00 2001 From: Christopher Boyd Date: Thu, 6 Aug 2020 09:43:57 -0500 Subject: [PATCH 6/9] Finished solutions to modulo, multiply and divide tests. --- src/recursion.js | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 94a76c638..9bfd17693 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -214,10 +214,14 @@ if (y === 1){ return x } +if (y === 0 || x === 0){ + return 0 +} + if (y > 0){ return x+multiply(x, y-1) -}else if (y <= 0 && x < 0){ - return x-multiply(x, y+1) // problem with -275, -502; returns 0. +}else if (y < 0 && x < 0){ + return multiply(x, y+1)-x }else if (y < 0){ return x+multiply(x, y+1) } @@ -227,6 +231,45 @@ if (y > 0){ // 13. Write a function that divides two numbers without using the / operator or // JavaScript's Math object. var divide = function(x, y) { + +if (y === 0){ + return NaN +} + +if (x === 0){ + return 0 +} + +if (y === 1){ + return x +} + +if (x < 0 && y < 0){ + if (y < x){ + return 0 + } +} + +if (y > x){ + return 0 +} + +if (y === x){ + return 1 +} + +if (x > 0 && y > 0){ + return 1+divide(x-y, y) +} + +if (x < 0 && y < 0){ + return 1+divide(x-y, y) +} + +if (x < 0 && y > 0){ + return 1+divide(x+y, y) +} + }; // 14. Find the greatest common divisor (gcd) of two positive numbers. The GCD of two From 051c309cfabff680bbce5fead554caf56ed1e8be Mon Sep 17 00:00:00 2001 From: Justin Nguyen Date: Thu, 6 Aug 2020 10:44:44 -0500 Subject: [PATCH 7/9] Finished Recursion --- src/recursion.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index 9bfd17693..382d0fb9f 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -278,6 +278,29 @@ if (x < 0 && y > 0){ // 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) { + if (Math.sign(x) === -1 || Math.sign(y) === -1) { + return null + } + + if (x === 0){ + return y + } + + if(y === 0){ + return x + } + + if (x > y){ + if(x === (y * ~~(x/y)+(x % y))){ + return gcd(y, (x % y)) + } + } + + if (y > x){ + if(y === (x * ~~(y/x)+(y % x))){ + return gcd(x, (y % x)) + } + }
 }; // 15. Write a function that compares each character of two strings and returns true if @@ -286,6 +309,23 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { + if (Array.isArray(str1) && Array.isArray(str2)){ + + if (str1.length === str2.length){ + + for (i = 0; i < str1.length; i++){ + if (str2[i] !== str1[i]){ + return false + } + } + + return true + }else{ + return false + } + }else{ + + return compareStr(str1.split(""), str2.split(""))} }; // 16. Write a function that accepts a string and creates an array where each letter From a1b456340cae1124d6a6c5eae39dba1a55e837b9 Mon Sep 17 00:00:00 2001 From: Justin Nguyen Date: Thu, 6 Aug 2020 10:44:44 -0500 Subject: [PATCH 8/9] Finished Recursion --- src/recursion.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index 9bfd17693..36ae2a998 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -278,6 +278,29 @@ if (x < 0 && y > 0){ // 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) { + if (Math.sign(x) === -1 || Math.sign(y) === -1) { + return null + } + + if (x === 0){ + return y + } + + if(y === 0){ + return x + } + + if (x > y){ + if(x === (y * ~~(x/y)+(x % y))){ + return gcd(y, (x % y)) + } + } + + if (y > x){ + if(y === (x * ~~(y/x)+(y % x))){ + return gcd(x, (y % x)) + } + }
 }; // 15. Write a function that compares each character of two strings and returns true if @@ -286,6 +309,22 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { + if (Array.isArray(str1) && Array.isArray(str2)){ + if (str1.length === str2.length){ + for (i = 0; i < str1.length; i++){ + if (str2[i] !== str1[i]){ + return false + } + } + return true + } + else { + return false + } + } + else { + + return compareStr(str1.split(""), str2.split(""))} }; // 16. Write a function that accepts a string and creates an array where each letter From 6b0b4d6f5e81e2ed19d90dd977f3c842ba7a3ddb Mon Sep 17 00:00:00 2001 From: Justin Nguyen Date: Thu, 6 Aug 2020 14:28:22 -0500 Subject: [PATCH 9/9] Extra Work Done --- src/recursion.js | 275 ++++++++++++++++++++++++++++++----------------- 1 file changed, 175 insertions(+), 100 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 36ae2a998..85c3cd8f3 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -6,11 +6,11 @@ // factorial(5); // 120 var factorial = function(n) { - if (Math.sign(n) === -1){ + if (Math.sign(n) === -1) { return null } - if (n === 0){ + if (n === 0) { return 1 } @@ -21,11 +21,11 @@ var factorial = function(n) { // Example: sum([1, 2, 3, 4, 5, 6]); // 21 var sum = function(array) { - if (!array.length){ + if (!array.length) { return 0 } - return array[0]+sum(array.slice(1)) + return array[0] + sum(array.slice(1)) }; // 3. Sum all numbers in an array containing nested arrays. @@ -36,15 +36,15 @@ var arraySum = function(array) { return 0 } - if (Array.isArray(array[0])){ + if (Array.isArray(array[0])) { var sum = arraySum(array[0]) // continue logic here } + else { var sum = array[0] } return sum + arraySum(array.slice(1)) - }; // 4. Check if a number is even. @@ -52,12 +52,15 @@ var isEven = function(n) { if (n === 0) { return true } + else if (n === 1) { return false } + if (Math.sign(n) === -1) { return isEven(n + 2) } + else { return isEven(n - 2) } @@ -70,16 +73,23 @@ var sumBelow = function(n) { if (n === 0) { return 0 } - if (Math.sign(n) === -1){ - var currentN = n + 1} - else{ - var currentN = n - 1} - if (currentN === 0){ + + if (Math.sign(n) === -1) { + var currentN = n + 1 + } + + else { + var currentN = n - 1 + } + + if (currentN === 0) { return 0 } + if (Math.sign(currentN) === -1) { return currentN + sumBelow(currentN) } + else { return currentN + sumBelow(currentN) } @@ -88,21 +98,20 @@ 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) { - //debugger; - if (x === y || x-1 === y || x+1 === y || y === undefined){ + if (x === y || x - 1 === y || x + 1 === y || y === undefined) { return[] } - if(x < y){ - var nextX = x+1 + if (x < y) { + var nextX = x + 1 } - if(x > y){ - var nextX = x-1 + + if (x > y) { + var nextX = x - 1 } return [nextX].concat(range(nextX, y)) - }; // 7. Compute the exponent of a number. @@ -115,14 +124,17 @@ var exponent = function(base, exp) { if (exp === 0){ return 1 } - if (exp === 1){ + + if (exp === 1) { return base } - if (exp > 0){ - return base * exponent(base, exp-1) + + if (exp > 0) { + return base * exponent(base, exp - 1) } - if (exp < 0){ - return 1 / exponent(base, (-1*exp)) + + if (exp < 0) { + return 1 / exponent(base, (-1 * exp)) } }; @@ -132,9 +144,11 @@ var exponent = function(base, exp) { // powerOfTwo(10); // false var powerOfTwo = function(n) { - if(n === 1){ + if (n === 1) { return true - }else if(n % 2 !== 0 || n === 0){ + } + + else if (n % 2 !== 0 || n === 0) { return false } @@ -144,24 +158,27 @@ var powerOfTwo = function(n) { // 9. Write a function that accepts a string and reverses it. var reverse = function(string) { -if (Array.isArray(string)){ - var reversedText = string.reverse() - return reversedText.join("") -}else{ - var text = string - var textArr = text.split("") - return reverse(textArr) -} + if (Array.isArray(string)) { + var reversedText = string.reverse() + return reversedText.join("") + } + else { + var text = string + var textArr = text.split("") + return reverse(textArr) + } }; // 10. Write a function that determines if a string is a palindrome. var palindrome = function(string) { - if (Array.isArray(string)){ + if (Array.isArray(string)) { var reversedText = string.reverse() return reversedText.join("") - }else{ + } + + else { var correctString = string.toLowerCase() var correctString1 = correctString.replace(/\s+/g, ''); return (correctString1 === palindrome(correctString1.split(""))) @@ -175,33 +192,40 @@ var palindrome = function(string) { // modulo(17,5) // 2 // modulo(22,6) // 4 var modulo = function(x, y) { - if( x === 0 && y === 0){ + if ( x === 0 && y === 0){ return NaN } + if (x >= 0 && y >= 0){ - if (y > x){ + if (y > x) { return x - }else if(y <= x){ - return modulo(x-y, y) + } + else if(y <= x) { + return modulo(x - y, y) } } + if (x < 0 && y < 0){ - if (y < x){ + if (y < x) { return x - }else if(y >= x){ - return modulo(x-y, y) + } + else if(y >= x) { + return modulo(x - y, y) } } - if (x < 0 && y > 0){ + + if (x < 0 && y > 0) { if (y < x){ return x - }else if(y >= x){ - var negativeXModulo = modulo(y+x, y) - if(x < y && negativeXModulo > 0){ - return x - }else{ - return negativeXModulo - } + } + else if (y >= x) { + var negativeXModulo = modulo(y + x, y) + if (x < y && negativeXModulo > 0) { + return x + } + else { + return negativeXModulo + } } } }; @@ -210,20 +234,24 @@ var modulo = function(x, y) { // JavaScript's Math object. var multiply = function(x, y) { -if (y === 1){ +if (y === 1) { return x } -if (y === 0 || x === 0){ +if (y === 0 || x === 0) { return 0 } -if (y > 0){ - return x+multiply(x, y-1) -}else if (y < 0 && x < 0){ - return multiply(x, y+1)-x -}else if (y < 0){ - return x+multiply(x, y+1) +if (y > 0) { + return x + multiply(x, y - 1) +} + +else if (y < 0 && x < 0) { + return multiply(x, y + 1) - x +} + +else if (y < 0) { + return x + multiply(x, y + 1) } }; @@ -232,44 +260,43 @@ if (y > 0){ // JavaScript's Math object. var divide = function(x, y) { -if (y === 0){ - return NaN -} - -if (x === 0){ - return 0 -} - -if (y === 1){ - return x -} + if (y === 0) { + return NaN + } -if (x < 0 && y < 0){ - if (y < x){ + if (x === 0) { return 0 } -} -if (y > x){ - return 0 -} + if (y === 1) { + return x + } -if (y === x){ - return 1 -} + if (x < 0 && y < 0) { + if (y < x) { + return 0 + } + } -if (x > 0 && y > 0){ - return 1+divide(x-y, y) -} + if (y > x) { + return 0 + } -if (x < 0 && y < 0){ - return 1+divide(x-y, y) -} + if (y === x) { + return 1 + } -if (x < 0 && y > 0){ - return 1+divide(x+y, y) -} + if (x > 0 && y > 0) { + return 1 + divide(x - y, y) + } + if (x < 0 && y < 0) { + return 1 + divide(x - y, y) + } + + if (x < 0 && y > 0) { + return 1 + divide(x + y, y) + } }; // 14. Find the greatest common divisor (gcd) of two positive numbers. The GCD of two @@ -278,29 +305,30 @@ if (x < 0 && y > 0){ // 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) { + if (Math.sign(x) === -1 || Math.sign(y) === -1) { return null } - if (x === 0){ + if (x === 0) { return y } - if(y === 0){ + if(y === 0) { return x } if (x > y){ - if(x === (y * ~~(x/y)+(x % y))){ + if (x === (y * ~~(x/y) + (x % y))) { return gcd(y, (x % y)) } } - if (y > x){ - if(y === (x * ~~(y/x)+(y % x))){ + if (y > x) { + if (y === (x * ~~(y/x)+(y % x))) { return gcd(x, (y % x)) } - }
 + } }; // 15. Write a function that compares each character of two strings and returns true if @@ -309,10 +337,11 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { - if (Array.isArray(str1) && Array.isArray(str2)){ - if (str1.length === str2.length){ - for (i = 0; i < str1.length; i++){ - if (str2[i] !== str1[i]){ + + if (Array.isArray(str1) && Array.isArray(str2)) { + if (str1.length === str2.length) { + for (i = 0; i < str1.length; i++) { + if (str2[i] !== str1[i]) { return false } } @@ -321,31 +350,77 @@ var compareStr = function(str1, str2) { else { return false } - } - else { + } - return compareStr(str1.split(""), str2.split(""))} + else { + return compareStr(str1.split(""), str2.split("")) + } }; // 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){ + + if (Array.isArray(str)) { + return str + } + + else { + return createArray(str.split("")) + } }; // 17. Reverse the order of an array var reverseArr = function (array) { + + if (array[0] === undefined) { + array.shift() + return array + } + + var newArray = [] + for (var i = array.length; i >= 0; i--) { + newArray.push(array[i]) + } + return reverseArr(newArray) }; // 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) { + + if (length === 0) { + return [] + } + + return [value].concat(buildList(value, length - 1)) }; // 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) { + debugger; + var takeOutArray = [] + var counter = 0 + for (var i = 0; i < array.length; i++) { + if (array[i] === value) { + takeOutArray.push(value) + } + else { + counter++ + } + } + + if (counter === 0) { + return takeOutArray.length + } + + else { + return countOccurrence(takeOutArray, value) + } + }; // 20. Write a recursive version of map.