Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. // function sumOf(list){
  2. // if(list.length === 1){
  3. // return list[0];
  4. // }
  5. // return list[0] = sumOf(list.slice(1));
  6. // }
  7. //
  8. // let lst = [2,4,6,8,10];
  9. // console.log(sumOf(lst));
  10. //
  11. function countingSheep(num){
  12. if(num > 0){
  13. console.log(num + 'another sheep jumps over the fence');
  14. return countingSheep(num-1);
  15. }
  16. }
  17.  
  18. console.log(countingSheep(5));
  19.  
  20.  
  21.  
  22.  
  23. //double each number in an array recursively
  24. function doubleArray(arr, index = 0, newArr = []){
  25. if(index === arr.length-1){
  26. return newArr;
  27. }
  28. newArr.push(arr[index] * 2);
  29.  
  30. console.log('it is firing here');
  31. return doubleArray(arr, index + 1, newArr);
  32. }
  33.  
  34.  
  35. let lst = [2,4,6,8,10];
  36. console.log(doubleArray(lst, 0))
  37.  
  38.  
  39.  
  40.  
  41.  
  42. function fib(n){
  43. if(n < 2){
  44. return n;
  45. }
  46. return fib(n-1) + fib(n-2);
  47. }
  48.  
  49. function reverseString(str, i = str.length-1, newStr = ''){
  50. if(i === 0){
  51. return newStr;
  52. }
  53. newStr += str[i];
  54. return reverseString(str, i-1, newStr)
  55. }
  56. // console.log(reverseString('hello'));
  57.  
  58.  
  59. function triangleNumber(row){
  60. if(row === 1){
  61. return 0;
  62. }
  63. return row + triangleNumber(row-1);
  64. }
  65.  
  66. console.log(triangleNumber(3));
  67.  
  68.  
  69.  
  70.  
  71. ///create a split function using recursion
  72. function strSplitter(str, splitOn, index = 0, newStr1='', newStr2=''){
  73. if(index === str.length){
  74. let anArr = [];
  75. anArr.push(newStr1, newStr2)
  76. return anArr;
  77. }
  78. if(index < splitOn){
  79. newStr1 += str[index]
  80. } else if(index >= splitOn){
  81. newStr2 += str[index]
  82. }
  83. return strSplitter(str, splitOn, index + 1, newStr1, newStr2);
  84. }
  85.  
  86. console.log(strSplitter('Hello', 3));
  87.  
  88.  
  89. function factorial(num){
  90. if(num === 1){
  91. return num;
  92. }
  93. return num * factorial(num-1);
  94. }
  95. console.log(factorial(5));
  96.  
  97.  
  98. //anagrams
  99. let genAnagrams = (word, anagram = '', anagrams = []) => {
  100. anagrams.push(anagram);
  101. for (let i = 0; i < word.length; i++){
  102. genAnagrams(word.slice(0, i) + word.slice(i + 1), anagram + word[i], anagrams);
  103. }
  104.  
  105. return [...new Set(anagrams)];
  106. };
  107.  
  108. console.log(genAnagrams('ABC'));
  109.  
  110.  
  111. function getAllPermutations(string) {
  112. var results = [];
  113. for (var i = 0; i < string.length; i++) {
  114. var firstChar = string[i];
  115. var charsLeft = string.substring(0, i) + string.substring(i + 1);
  116. var innerPermutations = getAllPermutations(charsLeft);
  117. for (var j = 0; j < innerPermutations.length; j++) {
  118. results.push(firstChar + innerPermutations[j]);
  119. }
  120. }
  121. return results;
  122. }
  123.  
  124. // console.log(getAllPermutations('abc'));
  125.  
  126. const chart = ['Zuckerburg', ['Schoefer', ['Bosworth', ['Steve', 'Kyle', 'Andrea']], ['Zhao', ['Richie', 'Sofia', 'Jen']]],
  127. ['Schrage', ['VanDyck', ['Sabrina', 'Michelle', 'Josh']], ['Swain', ['Blanch', 'Tom', 'Joe']]],
  128. ['Sandberg', ['Goler', ['Eddie', 'Julie', 'Annie']], ['Hernandez', ['Rowi', 'Inga', 'Morgan']], ['Moissinac', ['Amy', 'Chuck', 'Vinni']], ['Kelley', ['Eric', 'Anna', 'Wes']]]
  129. ]
  130.  
  131.  
  132. function printChart(chart, index = 0, indent = ''){
  133.  
  134. for(let i = 0; i < chart.length; i++){
  135. if(typeof chart[i] === 'string'){
  136. console.log(indent + chart[i]);
  137. } else {
  138. printChart(chart[i], index++, indent +=' ')
  139. }
  140. }
  141. }
  142.  
  143. console.log(printChart(chart));
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement