Javascript questions
Type Utility 1
In this question, we will implement the following utility functions to determine the types of primitive values.
- isBoolean(value): Return true if value is a boolean, false otherwise.
- isNumber(value): Return true if value is a number, false otherwise. Note that NaN is considered a number.
- isNull(value): Return true if value is null, false otherwise.
- isString(value): Return true if value is a String, else false.
- isSymbol(value): Return true if value is a Symbol primitive, else false.
- isUndefined(value): Return true if value is undefined, else false.
Type Utility 2
we will implement the following utility functions to determine the types of non-primitive values.
- isArray(value): Return true if value is an array, false otherwise.
- isFunction(value): Return true if value is a function, false otherwise.
- isObject(value): Return true if value is an object (e.g. arrays, functions, objects, etc, but not including null and undefined), false otherwise.
- isPlainObject(value): Return true if value is a plain object, false otherwise (for arrays, functions, etc).
- A plain object, or what is commonly known as a Plain Old JavaScript Object (POJO) is any object whose prototype is Object.prototype or an object created via Object.create(null).
Flatten
const arr = [1,[2, 3,[4,5,6],[7]]]
MinBy
Implement a function minBy(array, iteratee) that finds the element inside array with the minimum value after going through iteratee.
Arguments
array (Array): The array to iterate over. iteratee (Function): The iteratee invoked per element, which is a function that accepts one argument: (value). Returns
(*): Returns the minimum value.
Examples
-
minBy([2, 3, 1, 4], (num) => num); // => 1
-
minBy([{ n: 1 }, { n: 2 }], (o) => o.n); // => { n: 1 }
The function should ignore elements where iteratee produces null or undefined.
- minBy([{ n: 1 }, { n: 2 }], (o) => o.m); // => undefined
Cycle
Implement a function that takes one or more values and returns a function that cycles through those values each time it is called.
const helloFn = cycle(‘hello’); console.log(helloFn()); // “hello” console.log(helloFn()); // “hello”
const onOffFn = cycle(‘on’, ‘off’); console.log(onOffFn()); // “on” console.log(onOffFn()); // “off” console.log(onOffFn()); // “on”
countBy
Implement a function countBy(array, iteratee) that creates an object composed of keys generated from the results of running each element of array through iteratee. The corresponding value of each key is the number of times the key was returned by iteratee.
countBy(array, iteratee); Arguments
array (Array): The array to iterate over. iteratee (Function): The iteratee function to transform elements. The function is invoked with one argument: (value). Returns
(Object): Returns the composed aggregate object.
Examples
countBy([6.1, 4.2, 6.3], Math.floor); // => { ‘4’: 1, ‘6’: 2 }
countBy([{ n: 3 }, { n: 5 }, { n: 3 }], (o) => o.n); // => { ‘3’: 2, ‘5’: 1 } The function should return when array is empty and treat null / undefined keys after going through iteratee as it is.
countBy([], (o) => o); // => {}
countBy([{ n: 1 }, { n: 2 }], (o) => o.m); // => { undefined: 2 }
Promisify
// Example function with callback as last argument
// The callback has the signature (err, value) => any
function foo(url, options, callback) {
apiCall(url, options)
.then((data) => callback(null, data))
.catch((err) => callback(err));
}
const promisifiedFoo = promisify(foo); const data = await promisifiedFoo(‘example.com’, { foo: 1 });
The .reduce() method
In simple terms, the .reduce() method takes into account a previous value , current value and an accumulator.
The return type of the .reduce() method is always a single value. It is useful when you want to process all the values of the array and want to derive some accumulated result.
Here, the currentObj is the object that is being iterated over. Also, the acc value stores the result and is outputted finally into the totalAge array.