Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question 1: Create an Array from 0...100 without using traditional For-loop
- function arrayList(){
- var i=0;
- return new Array(101).fill(0).map(()=>i++)
- }
- // Question 2: Create an Array of only even numbers from the above array
- function TwoOnly(){
- return arrayList().filter(x=>x%2 === 0)
- }
- // Create a function that returns a Promise which returns the
- // square of only even numbers and
- // throws an error if an odd number is passed
- function tell(x){
- return new Promise((resolve,reject)=>{
- if(x%2 == 0){
- resolve(x*x)
- }else{
- reject("Odd Number")
- }
- })
- }
- // Question 3: create an array of even squares using the previous array
- // direct invocation is important to see if the
- // candidate understands Arrow functions correctly.
- function squareOfEven(){
- return TwoOnly().map((x)=>x*x)
- }
- // Question 4: Sum of all the squares from the above array of Even Squares
- function totalSum(){
- return squareOfEven().reduce((initial,val)=>val=val+initial,0)
- }
- // Question 5: Call the above square Promise function with all numbers from 0-100
- // and ensure that the errors are not thrown
- // then print the following:
- // 1. Number of errors
- // 2. The resultant array
- // 3. Number of objects in the resultant array
- var arr=arrayList();
- var promiseArray=[]
- for(i=0;i<arr.length;i++){
- promiseArray.push(tell(arr[i]))
- }
- var num=0
- var i=0;
- Promise.all(promiseArray).then((x=>{
- return new Promise((resolve,reject)=>{
- x.forEach((y)=>{
- if(y==null){
- i++;
- }
- })
- resolve(i)
- })
- }))
- Promise.all(promiseArray).then((x=>{
- console.log("resultant array",x)
- }))
- // Q1: Create a template string of a button HTML Element. Save the title
- // for the button in a separate var and use it in the button correctly.
- function createButton(){
- var buttonTitle = "submit" ;
- return `<button>${buttonTitle}</button>`;
- }
- // Q2: Create a function that returns a list element (<li>) HTML
- // use this function to create a ordered list HTML structure of numbers from
- // 1-30 without breaking the template string invocation i.e. usage of += is disallowed.
- function getOrderedList(){
- var len = 30 ;
- var return_str = "" ;
- return_str = return_str.concat('' , '<ol>')
- for(let i = 1; i <= len ; i++){
- return_str = return_str.concat('' , `<li>${i}</li>`)
- }
- return_str = return_str.concat('\n' , '</ol>')
- return return_str
- }
- // Q3: Now only print the even numbers. Again, breaking the template string
- // invocation is disallowed
- function getEvenOrderedList(){
- var len = 30 ;
- var return_str = "" ;
- return_str = return_str.concat('' , `<ol>`)
- for(let i = 1; i <= len ; i++){
- if(i%2 == 0) return_str = return_str.concat('' , `<li>${i}</li>`)
- }
- return_str = return_str.concat('\n' , `</ol>`)
- return return_str
- }
- // Q4: Create a promise that simulates a delay of 1-3s randomly. Create an array of
- // 10 such promises. Only print the output when all promises have been resolved
- var len_of_arr = 10 ;
- var promise = [] ;
- var time_arr = [1,2,3] ;
- for(let i = 0 ; i < len_of_arr ; i++){
- promise.push(new Promise(function(resolve,reject){
- setTimeout(function(){ resolve(i) }, time_arr[Math.floor(Math.random() * time_arr.length)]*1000);
- }))
- }
- Promise.all(promise).then(function(resp){
- console.log(resp)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement