Knowee
Questions
Features
Study Tools

Complete the function checkCanIVote Takes 2 arguments1st argument time, which is the number of milliseconds after the function will resolve or rejectSecond argument is the age upon (also a number) which you will use to return the string based on logic mentioned belowThe function returns a promise, which will have 2 functions as arguments resolve and reject like any other promise.The resolve function should be called with the argument "You can vote" after x milliseconds if age is greater than or equal to 18The reject function should be called with the argument with "You can not vote" after x milliseconds if age less than 18Note:- You only have to implement the function, in the example itshows how your implemented question will be ran.InputFunction will take two arguments1) 1st argument will be a number which tells after how much milliseconds promise will be resolved or rejected.2) 2nd argument will be a number (age)OutputFunction returns a promise which resolves to "You can vote" or rejects to "You can not vote".If age >= 18 resolves to "You can vote" else rejects to "You can not vote".

Question

Complete the function checkCanIVote Takes 2 arguments1st argument time, which is the number of milliseconds after the function will resolve or rejectSecond argument is the age upon (also a number) which you will use to return the string based on logic mentioned belowThe function returns a promise, which will have 2 functions as arguments resolve and reject like any other promise.The resolve function should be called with the argument "You can vote" after x milliseconds if age is greater than or equal to 18The reject function should be called with the argument with "You can not vote" after x milliseconds if age less than 18Note:- You only have to implement the function, in the example itshows how your implemented question will be ran.InputFunction will take two arguments1) 1st argument will be a number which tells after how much milliseconds promise will be resolved or rejected.2) 2nd argument will be a number (age)OutputFunction returns a promise which resolves to "You can vote" or rejects to "You can not vote".If age >= 18 resolves to "You can vote" else rejects to "You can not vote".

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the JavaScript function that implements the logic you described:

function checkCanIVote(time, age) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            if (age >= 18) {
                resolve("You can vote");
            } else {
                reject("You can not vote");
            }
        }, time);
    });
}

This function creates a new Promise. The Promise constructor takes a function as an argument, which itself takes two arguments: resolve and reject, which are both functions. Inside this function, we use the setTimeout function to delay the execution of the code inside it by time milliseconds. If age is greater than or equal to 18, we call resolve with the string "You can vote". Otherwise, we call reject with the string "You can not vote".

This problem has been solved

Similar Questions

Complete the function promiseMeSuch that it takes a number as the first argument (time) and a string as the second argument(data).It returns a promise which resolves after time milliseconds and data is returned.Note:- You only have to implement the function, in the example itshows your implemented question will be run.InputFunction should take number as first argument and data to be returned as second.OutputResolves to the data given as inputExamplepromiseMe(200, 'hi').then(data=>{console.log(data) // prints hi})

29What is the purpose of the resolve parameter in a Promise?Review LaterIt executes when the Promise is resolvedIt executes when the Promise is rejectedIt handles the asynchronous processIt defines the initial state of the Promise

entry is invalid, 1 if they are under the age limit or 2 if they are over the age limit. The function is shown below. The variables age and agelimit will both be integers.01 function checkage(age, agelimit):02 if age < 0 then03 return 004 elseif age < 38 then05 return 106 else:07 return 208 endif09 end functionThe code executes, but it does not give the correct result when called with: checkage(30, 20)The function returns 1, but it should return 2. State the type of error in the code and explain the change that needs to be made for the function to work.[3]

This problem may have one or more correct answersIf you want to execute a function right after the return of some other function, then callbacks can be used.A function passed to another function as an argument is referred to as a callback function.A callback function can run before another function has finished.None of these.

Write a program to check whether a person is eligible for voting or not. (accept age from user)

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.