javascript
1.Write a function delay that accepts two arguments, a callback and the wait time in milliseconds. Delay should return a function that, when invoked waits for the specified amount of time before executing. HINT - research setTimeout();
2.Create a function saveOutput that accepts a function (that will accept one argument), and a string (that will act as a password). saveOutput will then return a function that behaves exactly like the passed-in function, except for when the password string is passed in as an argument. When this happens, the returned function will return an object with all previously passed-in arguments as keys, and the corresponding outputs as values.
3.Create a function dateStamp that accepts a function and returns a function. The returned function will accept whatever arguments the passed-in function accepts and return an object with a date key whose value is today's date (not including the time) represented as a human-readable string (see the Date object for conversion methods), and an output key that contains the result from invoking the passed-in function.
4.Create a function defineFirstArg that accepts a function and an argument. Also, the function being passed in will accept at least one argument. defineFirstArg will return a new function that invokes the passed-in function with the passed-in argument as the passed-in function's first argument. Additional arguments needed by the passed-in function will need to be passed into the returned function.
Q1)
function delay(func, wait, ...rest) {
function rundelay() {
func(...rest);
}
setTimeout(rundelay, wait);
}
Q2)
const saveOutput = (inputFunc, str) => {
let newOb = {};
return function (value) {
if (value === str){
return newOb;
}
else {
newOb[value] = inputFunc(value);
return inputFunc(value)
}
}
}
const valuedouble = function(n) { return n * 2; };
const valuedoubleAndLog = saveOutput(valuedouble, 'boo');
console.log(valuedoubleAndLog(2));
console.log(valuedoubleAndLog(9));
console.log(valuedoubleAndLog('boo'));
Q3)
const dateStamp = (inputFunc) =>
{
let today = new Date();
return (...args) =>
{
return {
date: today.toDateString(),
output: inputFunc(...args)
}
}
}
const stampdouble = dateStamp(n => n * 2);
console.log(stampdouble(4));
console.log(stampdouble(6));
const stampMax = dateStamp((x, y) => Math.max(x, y));
console.log(stampMax(-5, 6));
console.log(stampMax(1, 4));
Q4)
const defineFirstArg = (inputFunc, arg) => {
return function (...addtionalArgs) {
return inputFunc(arg, ...addtionalArgs)
}
}
f2 = defineFirstArg(console.log,"b")
f2("a","d",'c')
Get Answers For Free
Most questions answered within 1 hours.