Javascript - Utilities Flashcards
Write a function which returns another function and execute it after calling
function higherOrderFunction(){ function displayHello(){ console.log("Hello") } return displayHello; }
// driver code var func = higherOrderFunction(); func(); // Hello
Write a function which executes another function recieved as an argument
function callbackExecutor(callback){ if(typeof callback === "function"){ callback(); } }
// driver code function callbackFunc(){ console.log("Callback function executed"); }
callbackExecutor(callbackFunc); // Callback function executed
Create a function having no parameters declared and print all the arguments passed to it
(Method1)
function func(){ for(let key in arguments){ console.log(arguments[key]); } }
// driver code func(1, "Hello", true);
Create a function having no parameters declared and print all the arguments passed to it
(Method2)
function func(){ for(let value of arguments){ console.log(value); } }
// driver code func(1, "Hello", true);
Write a function which executes only if the number of arguments match the number of parameters the function is expecting
function func(a, b, c){ if(func.length === arguments.length){ console.log("Number of arguments passed match the expected arguments"); } else { throw new Error("Number of arguments passed do not match the expected arguments"); } }
Design a function which can recieve variable number of arguments in parameters and print them
function varArgsFunc(...params){ params.forEach(function(value, index){ console.log(index, ": ", value); }) }
// driver code varArgsFunc("Hello", ",", "World", "!!!");
Write a function which can return multiple values from a function
(Method Array)
function multipleValueReturnFunc(){ const a = 5, b = 10; return [a, b]; }
// driver code const [x, y] = multipleValueReturnFunc();
Write a function which can return multiple values from a function
(Method Obj)
function multipleValueReturnFunc(){ const a = 'Java', b = 'Script'; return { a, b }; }
// driver code const {x, y} = multipleValueReturnFunc();
Write a function which can return multiple values from a function
(Method Iterator)
function* multipleValueReturnFunc(){ const a = 5, b = 10; yield a; yield b; }
// driver code const iterator = multipleValueReturnFunc(); const x = iterator.next().value; const y = iterator.next().value;
Display all the keys of an object
Method “IN”
for(let key in obj){ if (obj.hasOwnProperty(key)) { console.log(key); } }
Display all the keys of an object
Method “OF”
for(let key of Object.keys(obj)){ if (obj.hasOwnProperty(key)) { console.log(key); } }
Display all the keys of an object
Method “FOR EACH”
Object.keys(obj).forEach((key) => { if (obj.hasOwnProperty(key)) { console.log(key); } });
Display all the values of an object
console.log(Object.values(obj));
Display all the values of an object
Method “OF Obj”
for(let value of Object.values(obj)){
console.log(value);
}
Display all the values of an object
Method “KEY IN”
for(let key in obj){ if (obj.hasOwnProperty(key)) { console.log(obj[key]); } }
Write a function which can check if a given object is empty or not
function isObjectEmpty(obj){ if(obj !== null && typeof obj !== "undefined" && typeof obj === "object") return Object.keys(obj).length === 0 && JSON.stringify(obj) === "{}"; else return false; }
Show the usage of ‘Object.entries’ to create an object from key value pairs
(Method “ARRAY”)
const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ]; const obj = Object.fromEntries(arr);
Show the usage of ‘Object.entries’ to create an object from key value pairs
(Method “MAP”)
const map = new Map([ ['foo', 'bar'], ['baz', 42] ]); const obj = Object.fromEntries(map);