JS03 Exercises
Practical Questions
- Write a function
doubleNumbers(arr)that takes an array of numbers and returns an array with all elements doubled.
function doubleNumbers(arr) {
return arr.map(number => number * 2);
}- Write a function
performOp(arr, op, n)that takes an array of numbers, an operator op (e.g. + or -) and a number n and applies operator op with number n to all elements of the array. For example performOp([1, 2, 3, 4], '+', 3) should return [4, 5, 6, 7].
function performOp(arr, op, n) {
return arr.map(item => {
switch (op) {
case '+':
return item + n;
case '-':
return item - n;
case '*':
return item * n;
case '/':
return item / n;
default:
throw new Error('Invalid operator');
}
});
}- Write a function
filterEven(arr)that takes an array of numbers and returns an array containing the even numbers from the original array.
function filterEven(arr) {
return arr.filter(number => number % 2 === 0);
}- Write a function
removeSmallStrings(arr, len)that takes an array of strings and removes all strings that have fewer than len characters.
function removeSmallStrings(arr, len) {
return arr.filter(str => str.length >= len);
}- Write a function
findFirstLargeString(arr, len)that takes an array of strings and finds the first string that has more than len characters.
function findFirstLargeString(arr, len) {
return arr.find(str => str.length > len);
}- Write a function
findFirstLargeStringIndex(arr, len)that takes an array of strings and finds the first index of the string that has more than len characters.
function findFirstLargeStringIndex(arr, len) {
return arr.findIndex(str => str.length > len);
}- Write a function
hasOnlyPositiveNumbers(arr)that takes an array of numbers and returns a boolean indicating whether all numbers were positive.
function hasOnlyPositiveNumbers(arr) {
return arr.every(number => number > 0);
}- Write a function
sum(arr)that takes an array of numbers and returns the sum of all numbers.
function sum(arr) {
return arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}- Write a function
prod(arr)that takes an array of numbers and returns the product of all numbers.
function prod(arr) {
return arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
}- Write a function
sumOfEvenSquares(arr)that takes an array of numbers and returns the sum of squares of all even numbers.
function sumOfEvenSquares(arr) {
return arr.filter(number => number % 2 === 0)
.reduce((accumulator, currentValue) => accumulator + currentValue * currentValue, 0);
}- Write a function
getOldPeople(arr, age)that takes an array of objects where each object represents a person with a name and age property and returns all people that are older than age.
function getOldPeople(arr, age) {
return arr.filter(person => person.age > age);
}- Write a function
getAverageAge(arr, city)that takes an array of objects where each object represents a person with a name, age and city property find the average age of all people that live in city.
function getAverageAge(arr, city) {
let filteredPeople = arr.filter(person => person.city === city);
let totalAge = filteredPeople.reduce((accumulator, currentValue) => accumulator + currentValue.age, 0);
return filteredPeople.length > 0 ? totalAge / filteredPeople.length : 0;
}Theoretical Questions
- What is a higher-order function?
A higher-order function is a function that either takes one or more functions
as arguments (i.e., it operates on other functions) or returns a function as
its result. This concept is a key part of functional programming in JavaScript
and allows for the creation of more abstract, flexible, and reusable code.
Examples include map, filter, reduce, and forEach which are methods on
arrays in JavaScript.
- Why are map and filter often better than writing everything in an imperative manner?
map and filter are often better than imperative code because they are more
readable, reduce boilerplate, promote immutability, allow easy chaining for
complex operations, and align well with functional programming principles,
making the code more maintainable and less error-prone.
- Explain the functionality behind the reduce function
The reduce function in JavaScript aggregates array elements into a single
value based on a function you provide. It takes a callback function and an
optional initial accumulator value. The callback runs for each array element,
taking the current accumulator value and the current array element as
arguments, and returns the updated accumulator. The final accumulator value
becomes the reduce function's result. This is particularly useful for
summing numbers, constructing objects, or combining arrays.
- How would you find the first element in an array that matches a certain predicate?
To find the first element in an array that matches a certain predicate, use
the find method with a callback function defining the predicate. It returns
the first matching element or undefined if none is found.
- How would you find the index of the first element in an array that matches a certain predicate?
To find the index of the first element in an array that matches a certain
predicate, use the findIndex method with a callback function defining the
predicate. It returns the index of the first matching element, or -1 if no
match is found.