Zhorzh

Untitled

Mar 15th, 2022
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. 1
  2. let n = +gets();
  3. const factorial = (n) => {
  4. if (n<2) return 1
  5. return n * factorial(n-1);
  6. }
  7. print(factorial(n))
  8.  
  9. 2
  10. const numberOfBunnies = +gets();
  11. const numberOfEars = (bunnies) => {
  12. if (bunnies === 0) return 0
  13. return 2 + numberOfEars(bunnies-1)
  14. }
  15. print(numberOfEars(numberOfBunnies))
  16.  
  17. 4
  18. const numberOfBunnies = +gets();
  19. const numberOfEars = (bunnies) => {
  20. if (bunnies === 0)return 0;
  21. return bunnies%2===0 ? 3 + numberOfEars(bunnies-1): 2 + numberOfEars(bunnies-1);
  22. }
  23. print(numberOfEars(numberOfBunnies))
  24.  
  25. 5
  26. const n = +gets();
  27. const pyramide = (n)=>{
  28. if(n === 0) return 0;
  29. return n+ pyramide(n-1);
  30. }
  31. print(pyramide(n))
  32.  
  33. 6
  34. const num = +gets();
  35. const sum = (n) => {
  36. if(n === 0) return 0;
  37. return n%10 + sum(Math.trunc(n/10))
  38. }
  39. print(sum(num))
  40.  
  41. 7
  42. let num = +gets();
  43. let count = 0;
  44. function sum(n) {
  45. if (n === 0) return 0;
  46. let first = (n % 10);
  47. if (first === 7) count++;
  48. sum(Math.trunc(n / 10));
  49. return count;
  50. };
  51. print(sum(num));
  52.  
  53. 8
  54. let num = +gets();
  55. let count = 0;
  56. function sum(n) {
  57. if (n === 0) return 0;
  58. let first = (n % 10);
  59. let second = (Math.trunc(n / 10)) % 10;
  60. if (first === 8 ) {
  61. if(first === second){
  62. count+=2;
  63. }else {
  64. count++
  65. }
  66. }
  67. sum(Math.trunc(n / 10))
  68. return count;
  69. };
  70. print(sum(num));
  71.  
  72. 9
  73. let num = +gets();
  74. let pows = +gets();
  75. const pow = (n, p) => {
  76. if(p === 0) return 1;
  77. return n* pow(n, p-1);
  78. }
  79. print(pow(num, pows))
  80.  
  81. 10
  82. let string = gets();
  83. let count = 0;
  84. const findX = (string) => {
  85. if(!string.length) return;
  86. if(string[0] === 'x') count++;
  87. const newString = string.slice(1);
  88. findX(newString)
  89. return count
  90. }
  91. print(findX(string))
  92.  
  93. 11
  94. let string = gets();
  95. let count = 0;
  96. const search = (string) => {
  97. if(!string.length) return;
  98. if (string[0] === 'h' && string[1] === 'i') count++
  99. const newString = string.slice(1);
  100. search(newString)
  101. return count
  102. }
  103. print(search(string))
  104.  
  105. 12
  106. let string = gets();
  107. let final = ''
  108. let newString = '';
  109. const search = (string) => {
  110. if(!string.length) return;
  111. if(string[0] === 'p' && string[1] === 'i') {
  112. final += '3.14'
  113. newString = string.slice(2);
  114. } else {
  115. final += string[0];
  116. newString = string.slice(1)
  117. }
  118. search(newString)
  119. return final
  120. }
  121. print(search(string))
  122.  
  123. 13
  124. const arrays = gets().split(',').map(Number);
  125. const index = +gets();
  126. const searchForSix = (arrays, index) => {
  127. if ( arrays[index] === 6) return true
  128. if ( index === arrays.length) return false;
  129. return searchForSix(arrays,index+1)
  130. }
  131. print(searchForSix(arrays, index))
  132.  
  133. 14
  134. const arrays = gets().split(',').map(Number);
  135. const index = +gets();
  136. let count = 0;
  137. const searchForSix = (arrays, index) => {
  138. if ( index === arrays.length) return;
  139. if ( arrays[index] === 11) count++
  140. searchForSix(arrays,index+1)
  141. return count
  142. }
  143.  
  144. 15
  145. const arrays = gets().split(',').map(Number);
  146. const index = +gets();
  147. const something = ( array , index) => {
  148. if ( index === array.length) return false;
  149. if (array[index]*10 === array[index+1]) return true;
  150. return something(array, index+1)
  151. }
  152. print(something(arrays, index))
Advertisement
Add Comment
Please, Sign In to add comment