Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // Question 1: Create an Array from 0...100 without using traditional For-loop
  2. function arrayList(){
  3. var i=0;
  4. return new Array(101).fill(0).map(()=>i++)
  5. }
  6.  
  7. // Question 2: Create an Array of only even numbers from the above array
  8. function TwoOnly(){
  9. return arrayList().filter(x=>x%2 === 0)
  10. }
  11.  
  12.  
  13. // Create a function that returns a Promise which returns the
  14. // square of only even numbers and
  15. // throws an error if an odd number is passed
  16. function tell(x){
  17. return new Promise((resolve,reject)=>{
  18. if(x%2 == 0){
  19. resolve(x*x)
  20. }else{
  21. reject("Odd Number")
  22. }
  23. })
  24. }
  25.  
  26. // Question 3: create an array of even squares using the previous array
  27. // direct invocation is important to see if the
  28. // candidate understands Arrow functions correctly.
  29. function squareOfEven(){
  30. return TwoOnly().map((x)=>x*x)
  31. }
  32.  
  33.  
  34. // Question 4: Sum of all the squares from the above array of Even Squares
  35. function totalSum(){
  36. return squareOfEven().reduce((initial,val)=>val=val+initial,0)
  37. }
  38.  
  39. // Question 5: Call the above square Promise function with all numbers from 0-100
  40. // and ensure that the errors are not thrown
  41. // then print the following:
  42. // 1. Number of errors
  43. // 2. The resultant array
  44. // 3. Number of objects in the resultant array
  45.  
  46. var arr=arrayList();
  47. var promiseArray=[]
  48. for(i=0;i<arr.length;i++){
  49. promiseArray.push(tell(arr[i]))
  50. }
  51. var num=0
  52. var i=0;
  53. Promise.all(promiseArray).then((x=>{
  54. return new Promise((resolve,reject)=>{
  55. x.forEach((y)=>{
  56. if(y==null){
  57. i++;
  58. }
  59. })
  60. resolve(i)
  61. })
  62. }))
  63.  
  64. Promise.all(promiseArray).then((x=>{
  65. console.log("resultant array",x)
  66. }))
  67.  
  68.  
  69.  
  70. // Q1: Create a template string of a button HTML Element. Save the title
  71. // for the button in a separate var and use it in the button correctly.
  72. function createButton(){
  73. var buttonTitle = "submit" ;
  74. return `<button>${buttonTitle}</button>`;
  75. }
  76.  
  77.  
  78. // Q2: Create a function that returns a list element (<li>) HTML
  79. // use this function to create a ordered list HTML structure of numbers from
  80. // 1-30 without breaking the template string invocation i.e. usage of += is disallowed.
  81. function getOrderedList(){
  82. var len = 30 ;
  83. var return_str = "" ;
  84. return_str = return_str.concat('' , '<ol>')
  85. for(let i = 1; i <= len ; i++){
  86. return_str = return_str.concat('' , `<li>${i}</li>`)
  87. }
  88. return_str = return_str.concat('\n' , '</ol>')
  89. return return_str
  90. }
  91.  
  92. // Q3: Now only print the even numbers. Again, breaking the template string
  93. // invocation is disallowed
  94. function getEvenOrderedList(){
  95. var len = 30 ;
  96. var return_str = "" ;
  97. return_str = return_str.concat('' , `<ol>`)
  98. for(let i = 1; i <= len ; i++){
  99. if(i%2 == 0) return_str = return_str.concat('' , `<li>${i}</li>`)
  100. }
  101. return_str = return_str.concat('\n' , `</ol>`)
  102. return return_str
  103. }
  104.  
  105.  
  106. // Q4: Create a promise that simulates a delay of 1-3s randomly. Create an array of
  107. // 10 such promises. Only print the output when all promises have been resolved
  108. var len_of_arr = 10 ;
  109. var promise = [] ;
  110. var time_arr = [1,2,3] ;
  111. for(let i = 0 ; i < len_of_arr ; i++){
  112. promise.push(new Promise(function(resolve,reject){
  113. setTimeout(function(){ resolve(i) }, time_arr[Math.floor(Math.random() * time_arr.length)]*1000);
  114. }))
  115. }
  116.  
  117. Promise.all(promise).then(function(resp){
  118. console.log(resp)
  119. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement