Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //#1
  2. function printTimeout(str, n){
  3. setTimeout(function() {
  4. console.log(str);
  5. },n*1000);
  6. }
  7. printTimeout('Hello', 10);
  8.  
  9. //#2
  10. function sumAll(n) {
  11. return n == 1? n : n + sumAll(n-1);
  12. }
  13. console.log(sumAll(2));
  14. console.log(sumAll(4));
  15.  
  16. //#3
  17. function bombTimer(str, time){
  18. let i = setInterval (function () {
  19. if (time >= 1){
  20. console.log(time--);
  21. }else{
  22. console.log(str);
  23. clearInterval(i);
  24. }
  25. },1000);
  26. }
  27. bombTimer('Booom', 3);
  28.  
  29. //#4
  30. function factorial(n) {
  31. return n == 1 ? n : n * factorial(n-1);
  32. }
  33. console.log(factorial(3));
  34. console.log(factorial(5));
  35.  
  36. //#5
  37. function bombTimer(str, time){
  38. setTimeout(function lime() {
  39. if (time >= 1){
  40. console.log(time--);
  41. setTimeout(lime,1000);
  42. }else{
  43. console.log(str);
  44. }
  45. },0);
  46. }
  47. bombTimer('Booom', 3);
  48.  
  49. //#6
  50. function filterNumbers(arr, maxNumber) {
  51. let filteredArr = [];
  52. for (let i = 0; i < arr.length; i++){
  53. if (arr[i] <= maxNumber){
  54. filteredArr.push(arr[i]);
  55. }
  56. }
  57. return filteredArr;
  58. }
  59. console.log(filterNumbers([1, 4, 8, 1, 20], 5));
  60.  
  61. //#7
  62. function minMax(arr){
  63. return 'max: ' + Math.max(...arr) + ' min: ' + Math.min(...arr);
  64. }
  65. console.log(minMax([1, 4, 8, 2, 20]));
  66.  
  67. #8
  68. function arrAvg(arr) {
  69. return arr.reduce((a,b) => a+b, 0)/arr.length;
  70. }
  71. console.log(arrAvg([1,4,2]));
  72.  
  73. //#9
  74. function concatFirstNestedArrays(arr){
  75. let joinArr = [].concat.apply([], arr);
  76. return joinArr;
  77. }
  78. console.log(concatFirstNestedArrays([[0, 1], [2, 3], [4, 5]]));
  79.  
  80. //#10
  81. function usersToObject(users){
  82. let newObject = {};
  83. for(i=0; i < users.length; i++){
  84. newObject[users[i]['id']] = users[i];
  85. }
  86. return newObject;
  87. }
  88. const users = [
  89. { id: 1, name: 'John', birthday: '1999-2-12'},
  90. { id: 2, name: 'Bill', birthday: '1999-1-19'},
  91. { id: 3, name: 'Carol', birthday: '1999-1-11'},
  92. { id: 4, name: 'Luce', birthday: '1999-2-22'}
  93. ]
  94. console.log(usersToObject(users));
  95.  
  96. //#11
  97. function filterUsersByMonth(users, month){
  98. return users.filter(function(elem){
  99. if ((elem['birthday'].split('-'))[1] == month){
  100. return true;
  101. }else{
  102. return false;
  103. }
  104. });
  105. }
  106. const users = [
  107. { name: 'John', birthday: '1999-2-12' },
  108. { name: 'Bill', birthday: '1999-1-19' },
  109. { name: 'Carol', birthday: '1999-0-11' },
  110. { name: 'Luce', birthday: '1999-2-22' }
  111. ]
  112. console.log(filterUsersByMonth(users, 0));
  113.  
  114. //#12
  115. function getAdultNames(users) {
  116. let adultNames = [];
  117. for (i = 0; i < users.length; i++){
  118. let age = new Date().getFullYear() - new Date(users[i].birthday).getFullYear();
  119. if(age>=18){
  120. users[i].birthday = age;
  121. adultNames.push(Object.values(users[i]).join(' '));
  122. }
  123. }
  124. return adultNames.join(', ');
  125. }
  126. const users = [
  127. { name: 'John', birthday: '1999-6-12' },
  128. { name: 'Bill', birthday: '2005-5-19' },
  129. { name: 'Carol', birthday: '2003-10-11' },
  130. { name: 'Luce', birthday: '2000-11-22' }
  131. ];
  132. console.log(getAdultNames(users));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement