From dd3ff4886cd3c631cdb46df0ddcf4f7e7ecdad99 Mon Sep 17 00:00:00 2001 From: hunter nguyen Date: Thu, 20 Sep 2018 15:52:12 -0500 Subject: [PATCH 01/10] working on recursion --- src/recursion.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/recursion.js b/src/recursion.js index 12ca0a3d7..173d07a43 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -4,27 +4,57 @@ // 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) { + + if(i<0){ + return null; + }else if(i === 0){ + return total; + } + 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) { + if(array.length === 0){ + return 0; + } + if(array.length === 1){ + return array[0]; + } + for(let i = 0;i Date: Thu, 20 Sep 2018 22:02:46 -0600 Subject: [PATCH 02/10] recursion work --- src/recursion.js | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 173d07a43..f934d6a47 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -43,11 +43,14 @@ var arraySum = function(array) { // 4. Check if a number is even. var isEven = function(n) { - if(n/2 === n*2){ - return true; - }else{ + if(n & 1){ return false; + }else{ + return true; } + //do something + + }; // 5. Sum all integers below a given integer. @@ -60,6 +63,10 @@ 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) { + let arr = []; + + + return arr; }; // 7. Compute the exponent of a number. @@ -68,6 +75,15 @@ 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) { + let num; + if(exp === 0){ + return 1; + } + if(exp === 1){ + return base; + } + num = base * base; //exponent times + return num; }; // 8. Determine if a number is a power of two. @@ -75,14 +91,24 @@ var exponent = function(base, exp) { // powerOfTwo(16); // true // powerOfTwo(10); // false var powerOfTwo = function(n) { + // if(n*n === answer){ + // return true + // } }; // 9. Write a function that accepts a string a reverses it. var reverse = function(string) { + let str = "" + for(let i = string.length-1; i>=0;i--){ + str = str + string[i]; + } + + return str; }; // 10. Write a function that determines if a string is a palindrome. var palindrome = function(string) { + //use reverse }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -96,6 +122,9 @@ 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) { + let num; + num = x + y; //x +x by y times + return num; }; // 13. Write a function that divides two numbers without using the / operator or @@ -117,15 +146,26 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { + // if(str1.charAt() === str2.charAt){ + // 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){ + let arr =[]; + arr.push(str.split("")); + return arr; }; // 17. Reverse the order of an array var reverseArr = function (array) { + let arr = []; + for(let i = array.length-1;i>=0;i--){ + arr.push(array[i]); + } + return arr; }; // 18. Create a new array with a given value and length. @@ -138,6 +178,18 @@ var buildList = function(value, length) { // countOccurrence([2,7,4,4,1,4], 4) // 3 // countOccurrence([2,'banana',4,4,1,'banana'], 'banana') // 2 var countOccurrence = function(array, value) { + let num; + let arr = []; + for(let i = 0; i Date: Fri, 21 Sep 2018 15:55:48 -0500 Subject: [PATCH 03/10] recursion work --- src/recursion.js | 110 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 89 insertions(+), 21 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 173d07a43..0a37ecbbd 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -19,19 +19,13 @@ 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) { - if(array.length === 0){ - return 0; - } - if(array.length === 1){ - return array[0]; - } - for(let i = 0;i= array.length){ + return total; } + total += array[i]; + i++; + return sum(array, total, i); }; @@ -43,23 +37,73 @@ var arraySum = function(array) { // 4. Check if a number is even. var isEven = function(n) { - if(n/2 === n*2){ - return true; - }else{ - return false; - } + // if(n & 1){ + // return false; + // } +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) { - return 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, arr = [], i =x) { + + + + if(x === y || x === y+1 ){ + return arr; + } + if(x > y){ + i--; + } + else if(x < y){ + i++; + } + if (i === y) { + return arr; + } + + arr.push(i); + + + + return range(x,y, arr, i); + + + }; // 7. Compute the exponent of a number. @@ -67,7 +111,28 @@ 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(exp<0){ + total = 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. @@ -75,6 +140,9 @@ var exponent = function(base, exp) { // powerOfTwo(16); // true // powerOfTwo(10); // false var powerOfTwo = function(n) { + + + //return powerOfTwo(n); }; // 9. Write a function that accepts a string a reverses it. From 6727addc047b20c2472305a24da579f543fe9649 Mon Sep 17 00:00:00 2001 From: hunter nguyen Date: Mon, 24 Sep 2018 15:59:16 -0500 Subject: [PATCH 04/10] recursion work --- src/recursion.js | 66 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 0a37ecbbd..610289952 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -139,18 +139,60 @@ var exponent = function(base, exp, i= exp, total=1) { // powerOfTwo(1); // true // powerOfTwo(16); // true // powerOfTwo(10); // false -var powerOfTwo = function(n) { +var powerOfTwo = function(n, total = n) { + if(n === 0){ + return false; + } + if(n === 1){ + return true; + } + if(n< 0){ + return false; + } + if(n === 2){ + return true; + } + + if(total === 2){ + return true; + }else if(total < 2){ + return false; + } + total = total/2; - //return powerOfTwo(n); + + return powerOfTwo(n, total); }; // 9. Write a function that accepts a string a reverses it. var reverse = function(string) { + if(string === ''){ + return ''; + }else{ + return reverse (string.substring(1)) +string.charAt(0); + + } + + }; // 10. Write a function that determines if a string is a palindrome. var palindrome = function(string) { + string = string[0].toLowerCase(); + console.log('hello'); + string = string.split(''); + console.log(string); + + if(string[0] == string[string.length-1]){ + string = string.splice(0,1); + string = string.splice(string.length-1,1); + console.log(string); + return true; + } + + + return palindrome(string); }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -163,7 +205,25 @@ 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=0, total) { + if(x === 0 || y===0){ + return 0; + } + if(x<0 && y<0){ + total = (x - x) ; + //increment or decrement + } + if(x<0 || y< 0){ + total = x - x; + //increment or decrement + } + if(y === i){ + return total; + } + total = x + x; + i++; + + return multiply(x,y,i, total); }; // 13. Write a function that divides two numbers without using the / operator or From 6c1767b16a85ca45320f5c06eadc68ecfb9c2825 Mon Sep 17 00:00:00 2001 From: hunter nguyen Date: Tue, 25 Sep 2018 15:53:06 -0500 Subject: [PATCH 05/10] recursion work --- src/recursion.js | 88 ++++++++++++++++++++++++++++-------------------- 1 file changed, 52 insertions(+), 36 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 7b3382ae1..23cb8a2fb 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -177,22 +177,16 @@ var reverse = function(string) { }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string) { +var palindrome = function(string, value = false) { string = string[0].toLowerCase(); - //console.log('hello'); - string = string.split(''); - //console.log(string); - if(string === undefined){ + string = string.split(''); return true; - } - - string = string.splice(0, 1); - string = string.splice(string.length - 1, 1); + //return palindrome(string); - return palindrome(string); + }; // 11. Write a function that returns the remainder of x divided by y without using the @@ -245,50 +239,75 @@ var gcd = function(x, y) { // compareStr('', '') // true // compareStr('tomato', 'tomato') // true var compareStr = function(str1, str2) { - // if(str1.charAt() === str2.charAt){ - // return true; - // } + + + + //return compareStr(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){ - let arr =[]; - arr.push(str.split("")); - return arr; +var createArray = function(str, arr = [], num = 0){ + + if(num === str.length){ + return arr; + } + + arr.push(str.charAt(num)); + num++; + + return createArray(str,arr, num); }; // 17. Reverse the order of an array -var reverseArr = function (array) { - let arr = []; - for(let i = array.length-1;i>=0;i--){ - arr.push(array[i]); +var reverseArr = function (array, arr = []) { + + if(array.length === 0){ + return arr; } - return arr; + + if(array.length > 0 ){ + arr.push(array[array.length-1]); + } + + array.splice(array.length-1,1); + + return reverseArr(array, arr); }; // 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, arr = []) { + + if(value === length){ + return arr; + } + + if(value !== length){ + arr.push(value); + } + + + + return buildList(value, length, arr); }; // 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) { - let num; - let arr = []; - for(let i = 0; i Date: Wed, 26 Sep 2018 15:56:26 -0500 Subject: [PATCH 06/10] almost done recursion --- src/recursion.js | 118 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 102 insertions(+), 16 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 23cb8a2fb..e42cb1c82 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -238,11 +238,27 @@ 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(str1[i] !== str2[i]){ + return false; + } + + if (i === str1.length && i === str2.length) { + return true; + } + + + if(str1[i] === str2[i]){ + i++; + } - //return compareStr(str1, str2) + return compareStr(str1, str2, i); + + + }; // 16. Write a function that accepts a string and creates an array where each letter @@ -278,19 +294,24 @@ var reverseArr = function (array, arr = []) { // 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, arr = []) { +var buildList = function (value, length, arr = [], i = 0) { + - if(value === length){ + if (value === undefined) { + return [undefined]; + } + if (i === length) { return arr; } - if(value !== length){ + if (arr[i] !== value) { arr.push(value); + } + i++; - - return buildList(value, length, arr); + return buildList(value, length, arr, i); }; // 19. Count the occurence of a value inside a list. @@ -313,6 +334,7 @@ var countOccurrence = function(array, value, count = 0, i = 0) { // 20. Write a recursive version of map. // rMap([1,2,3], timesTwo); // [2,4,6] var rMap = function(array, callback) { + }; // 21. Write a function that counts the number of times a key occurs in an object. @@ -347,22 +369,68 @@ var fibonacci = function(n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function(n) { +var nthFibo = function(n, next = 0, arr = [0], i = 1) { + if(n<0){ + return null; + } + + if(arr[n]){ + return arr[n]; + } + + + + next = next+ i; + i=next; + + arr.push(next); + + return nthFibo(n, next, arr,i); }; // 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; + } + // if(input.length > 0 ){ + // input[i].toUpperCase(); + // } + + // i++; + + arr.push(input[i].toUpperCase()); + i++; + + return capitalizeWords(input, i, arr); - return 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, i = 0, arr = [],n = 1,x,y,z) { + if(i === array.length){ + return arr; + } + + + // x = array[i].charAt(0).toUpperCase(); + x = array[i].slice(0,1).toUpperCase(); + y = array[i].slice(1); + //concat/join it together + + z = x.concat(y); + arr.push(z); + i++; + n++; + + //access element in array then charAt(0).touppercase + //increment up + + return capitalizeFirst(array, i, arr, n,x,y,z); }; // 28. Return the sum of all even numbers in an object containing nested objects. @@ -384,7 +452,18 @@ 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, object = {}, tally = 1) { + + // if(letter is not in object){ + // object.letter = tally; + // + // } + //tally++ only if letter is seen again + + + return object; + + //return letterTally(str, obj,object,tally); }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -392,7 +471,8 @@ 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, arr = []) { + return arr; }; // 32. Augment every element in a list with a new value where each element is an array @@ -404,14 +484,20 @@ 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 = []) { + return arr; }; // 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 = []) { + return arr; + + + + }; // 35. Given a string, return a string with digits converted to their word equivalent. From e93a745da34f9247598e1f33d3b52e87d3c43b09 Mon Sep 17 00:00:00 2001 From: hunter nguyen Date: Thu, 27 Sep 2018 15:55:49 -0500 Subject: [PATCH 07/10] 8 more to go in recursion --- src/recursion.js | 69 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 14 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index e42cb1c82..00b4d55b1 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -334,6 +334,12 @@ var countOccurrence = function(array, value, count = 0, i = 0) { // 20. Write a recursive version of map. // rMap([1,2,3], timesTwo); // [2,4,6] var rMap = function(array, callback) { + if (array.length === 1) { + return callback(array); + } + else { + return [callback(array[0])].concat(rMap(array.slice(1), callback)) + } }; @@ -452,18 +458,26 @@ 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, object = {}, tally = 1) { - - // if(letter is not in object){ - // object.letter = tally; - // - // } - //tally++ only if letter is seen again +var letterTally = function(str, obj = {}, tally = 0, i = 0) { + let letters = str.split(''); + + + let arrOfObjKeys = Object.keys(obj); - return object; + if (i === letters.length) { + + return obj; + } + - //return letterTally(str, obj,object,tally); + if (!obj[letters[i]]){ + obj[letters[i]] = 1; + }else{ + obj[letters[i]] += 1; + } + i++; + return letterTally(str, obj, tally, i); }; // 31. Eliminate consecutive duplicates in a list. If the list contains repeated @@ -471,8 +485,17 @@ var letterTally = function(str, obj, object = {}, tally = 1) { // elements should not be changed. // Example: compress([1, 2, 2, 3, 4, 4, 5, 5, 5]) // [1, 2, 3, 4, 5] // Example: compress([1, 2, 2, 3, 4, 4, 2, 5, 5, 5, 4, 4]) // [1, 2, 3, 4, 2, 5, 4] -var compress = function(list, arr = []) { - return arr; +var compress = function(list, arr = [],i = 0) { + + if(i === list.length){ + return arr; + } + if(list[i] ){ + 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 @@ -484,19 +507,37 @@ var augmentElements = function(array, aug) { // 33. Reduce a series of zeroes to a single 0. // minimizeZeroes([2,0,0,0,1,4]) // [2,0,1,4] // minimizeZeroes([2,0,0,0,1,0,0,4]) // [2,0,1,0,4] -var minimizeZeroes = function(array, arr = []) { - return arr; +var minimizeZeroes = function(array, arr = [], i= 0, n =1) { + + if(i === array.length){ + return arr; + } + + if(array[i] !== array[n]){ + arr.push(array[i]); + } + + + + i++; + n++; + + return minimizeZeroes(array, arr, i , n) }; // 34. Alternate the numbers in an array between positive and negative regardless of // their original sign. The first number in the index always needs to be positive. // alternateSign([2,7,8,3,1,4]) // [2,-7,8,-3,1,-4] // alternateSign([-2,-7,8,3,-1,4]) // [2,-7,8,-3,1,-4] -var alternateSign = function(array, arr = []) { +var alternateSign = function(array, arr = [], i = 0) { return arr; + // arr.push(array[i]); + // i++; + + //return alternateSign(array, arr,i); }; From a557b5f117f0f7e23cd05afc83f959c071542afa Mon Sep 17 00:00:00 2001 From: hunter nguyen Date: Fri, 28 Sep 2018 15:55:04 -0500 Subject: [PATCH 08/10] 3 more tests --- src/recursion.js | 90 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 00b4d55b1..817f304be 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -177,14 +177,25 @@ var reverse = function(string) { }; // 10. Write a function that determines if a string is a palindrome. -var palindrome = function(string, value = false) { - string = string[0].toLowerCase(); +var palindrome = function(string) { - string = string.split(''); + if (string.length === undefined) { + return false; + } + + if (string[0].toLowerCase() === string[string.length - 1].toLowerCase()) { + return true; + } + - return true; + + + + string = string.slice(0,1); + string = string.slice(string.length-1,1) + - //return palindrome(string); + return palindrome(string); }; @@ -203,17 +214,25 @@ var multiply = function(x, y, i=0, total) { if(x === 0 || y===0){ return 0; } + if (i === y) { + return total; + } + if(x === 1){ + return y; + } + if(y ===1){ + return x; + } if(x<0 && y<0){ total = (x - x) ; - //increment or decrement + i--; } if(x<0 || y< 0){ - total = x - x; - //increment or decrement - } - if(y === i){ - return total; + total = x + x; + i++; } + + total = x + x; i++; @@ -375,23 +394,25 @@ var fibonacci = function(n) { // nthFibo(5); // 5 // nthFibo(7); // 13 // nthFibo(3); // 2 -var nthFibo = function(n, next = 0, arr = [0], i = 1) { - if(n<0){ +var nthFibo = function (n, fibSeq = [0, 1]) { + let nexnum = fibSeq[fibSeq.length - 1] + fibSeq[fibSeq.length - 2]; + fibSeq.push(nexnum); + + + if (n < 0) { return null; } - - if(arr[n]){ - return arr[n]; + if (!n) { + return 0; } - - - next = next+ i; - i=next; - arr.push(next); + if (fibSeq.length - 2 === n) { + return fibSeq[n]; + } else { + return nthFibo(n, fibSeq); + } - return nthFibo(n, next, arr,i); }; // 26. Given an array of words, return a new array containing each word capitalized. @@ -490,7 +511,7 @@ var compress = function(list, arr = [],i = 0) { if(i === list.length){ return arr; } - if(list[i] ){ + if(list[i] !== arr[arr.length-1]){ arr.push(list[i]); } @@ -529,16 +550,29 @@ var minimizeZeroes = function(array, arr = [], i= 0, n =1) { // 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, arr = [], i = 0) { - return arr; +var alternateSign = function (array, i = 0) { + i++; + var el = array[i]; + if (i === array.length) { + return array; + } - // arr.push(array[i]); - // i++; + if (array[0] < 0) { + array[0] *= -1; + } + + + if (el > 0 && i % 2 === 1) { + array[i] = array[i] * -1; + } + if (el < 0 && i % 2 === 0) { + array[i] *= -1; + } - //return alternateSign(array, arr,i); + return alternateSign(array, i); }; // 35. Given a string, return a string with digits converted to their word equivalent. From e03d39dfea0645845f8535317f50dba72971c884 Mon Sep 17 00:00:00 2001 From: Half Day Date: Sat, 6 Oct 2018 09:04:27 -0700 Subject: [PATCH 09/10] done with palindrome --- src/recursion.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index 817f304be..c591984f0 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -179,22 +179,18 @@ var reverse = function(string) { // 10. Write a function that determines if a string is a palindrome. var palindrome = function(string) { - if (string.length === undefined) { - return false; + if (string.length <= 1) { + return true; } - if (string[0].toLowerCase() === string[string.length - 1].toLowerCase()) { - return true; + if (string[0].toLowerCase() !== string[string.length - 1].toLowerCase()) { + return false; + } + string = string.substr(1,string.length-2); + string = string.replace(' ' , ''); - - - - string = string.slice(0,1); - string = string.slice(string.length-1,1) - - return palindrome(string); @@ -223,6 +219,7 @@ var multiply = function(x, y, i=0, total) { if(y ===1){ return x; } + if(x<0 && y<0){ total = (x - x) ; i--; @@ -232,6 +229,7 @@ var multiply = function(x, y, i=0, total) { i++; } + total = x + x; i++; From a3d7524fdb4135ce1ad74b580ce0b39793f2d67b Mon Sep 17 00:00:00 2001 From: Half Day Date: Fri, 26 Oct 2018 15:36:45 -0500 Subject: [PATCH 10/10] DONE --- src/recursion.js | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/recursion.js b/src/recursion.js index c591984f0..bf851dbba 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -206,35 +206,26 @@ var modulo = function(x, y) { // 12. Write a function that multiplies two numbers without using the * operator or // JavaScript's Math object. -var multiply = function(x, y, i=0, total) { +var multiply = function(x, y) { if(x === 0 || y===0){ return 0; } - if (i === y) { - return total; + if (y === 1) { + return x; } if(x === 1){ return y; } - if(y ===1){ + if(y === 1){ return x; } - - if(x<0 && y<0){ - total = (x - x) ; - i--; - } - if(x<0 || y< 0){ - total = x + x; - i++; + if(y<0){ + return -multiply(x, -y); } - - - total = x + x; - i++; + y--; - return multiply(x,y,i, total); + return x + multiply(x, y); }; // 13. Write a function that divides two numbers without using the / operator or