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);
Connect 2 objects so that one object is prototypically connected to the other
(Method “setPrototype”)
const obj1 = { a: 1 }; const obj2 = { b: 2 }; obj2.setPrototypeOf(obj1);
Connect 2 objects so that one object is prototypically connected to the other
(Method ‘__porto__’)
const obj1 = { a: "Object 1 value" }; const obj2 = { b: "Object 2 value" }; obj2.\_\_proto\_\_ = obj1;
Create an object with getter and setter for property
const obj = {};
Object.defineProperty(obj, 'data', { _data: 0, // closure variable to hold the data get() { return this._data; }, set(value) { this._data = value; } });
Write a class which uses private variable and function
class ClassWithPrivateFields { #privateVar; publicVar;
#privatFunc() { this.#privateVar = 7; this.publicVar = 10; }
publicFunc() { this.#privatFunc(); return [this.#privateVar, this.publicVar]; } }
// driver code const instance = new ClassWithPrivateFields();
// can't access private variable instance.privateVar; // undefined
// can’t access private function
instance. privatFunc(); // Error
instance. publicFunc(); // 7, 10
Show how can we use for..of loop to iterate on a range with given start and end values in an object
// Example let range = { start: 1, end: 10 };
for (let i of range) console.log(i); // 1 2 3 4 5 6 7 8 9 10
Write a program to iterate over an array and print all the values of it
(Method “traditional for loop”)
for(let i =0; i < arr.length; i++){ console.log(arr[i]); }