Double All NumbersProblem StatementWrite a function `doubleNumbers(arr)` that uses `.map()` to return a new array where every number is doubled.Approach 1. Call the `.map()` method on the array. 2. In the callback, return `num * 2`. Solution`function doubleNumbers(arr) { return arr.map(num => num * 2);
Filter EvensProblem StatementWrite a function `getEvents(arr)` that uses `.filter()` to return only the even numbers.Approach 1. Call `.filter()` on the array. 2. Check if `num % 2 === 0`. Solution`function getEvens(arr) { return arr.filter(num => num % 2 === 0);
Sum with ReduceProblem StatementWrite a function `sumArr(arr)` that calculates the total sum of an array using `.reduce()`.Approach 1. Use `.reduce()` with an accumulator and the current value. 2. Initialize the accumulator at 0. Solution`function sumArr(arr) { return arr.reduce((acc, curr) => acc + curr, 0);
Find Object by PropertyProblem StatementWrite a function `findUser(users, id)` that uses `.find()` to return the user object with the matching id.Approach 1. Use the `.find()` method. 2. The condition should be `user.id === id`. Solution`function findUser(users, id) { return users.find(user => user.id === id);
Extract NamesProblem StatementWrite a function `getNames(users)` that returns an array of just the `name` properties from an array of user objects.Approach 1. Transform objects into strings using `.map()`. Solution`function getNames(users) { return users.map(user => user.name);
Find Index of ItemProblem StatementWrite a function `findPosition(arr, target)` that returns the index of `target` using `.findIndex()`.Approach 1. Use `.findIndex()` with a comparison. Solution`function findPosition(arr, target) { return arr.findIndex(item => item === target);
Check All AdultsProblem StatementWrite a function `allAdults(users)` that returns `true` if everyone in the array has `age >= 18` using `.every()`.Approach 1. Use `.every()` to check a condition across all elements. Solution`function allAdults(users) { return users.every(user => user.age >= 18);
Check Any ActiveProblem StatementWrite a function `anyActive(items)` that returns `true` if at least one item has `isActive: true` using `.some()`.Approach 1. Use `.some()` to see if any element matches. Solution`function anyActive(items) { return items.some(item => item.isActive);
Flatten 2D ArrayProblem StatementWrite a function `flatten(arr)` that converts a 2D array into a 1D array using `.flat()`.Approach 1. Modern JS has the `.flat()` method for this. Solution`function flatten(arr) { return arr.flat();
Sort DescendingProblem StatementWrite a function `sortDesc(numbers)` that sorts an array of numbers in descending order.Approach 1. `.sort()` without a function sorts alphabetically! 2. Provide a comparator: `(a, b) => b - a`. Solution`function sortDesc(numbers) { return [...numbers].sort((a, b) => b - a);
Unique by IDProblem StatementWrite a function `uniqueById(users)` that removes duplicate user objects based on their `id` property.Approach 1. Use `reduce` with an empty array. 2. Only push if the ID isn't already present in the accumulator. Solution`function uniqueById(users) { return users.reduce((acc, current) => { const x = acc.find(item => item.id === current.id); if (!x) { return acc.concat([current]);
Calculate AverageProblem StatementWrite a function `calcAverage(numbers)` that returns the average using `.reduce()`.Approach 1. Sum all numbers. 2. Divide by the array length (handle empty array!). Solution`function calcAverage(numbers) { if (numbers.length === 0) return 0; return numbers.reduce((a, b) => a + b, 0) / numbers.length;
Group by PropertyProblem StatementWrite a function `groupBy(arr, prop)` that groups objects into an object where keys are property values.Approach 1. Use `.reduce()` to build an object. 2. For each item, use its property value as a key in the object. Solution`function groupBy(arr, prop) { return arr.reduce((acc, obj) => { const key = obj[prop]; if (!acc[key]) acc[key] = []; acc[key].push(obj); return acc;
Chunk ArrayProblem StatementWrite a function `chunk(arr, size)` that splits an array into smaller arrays of the specified size.Approach 1. Loop through the array. 2. Use `slice(i, i + size)` to grab chunks and push to a result array. Solution`function chunk(arr, size) { const result = []; for (let i = 0; i < arr.length; i += size) { result.push(arr.slice(i, i + size));
Array IntersectionProblem StatementWrite a function `getIntersection(arr1, arr2)` that returns elements common to both arrays.Approach 1. Filter the first array. 2. Check if the second array includes the current item. Solution`function getIntersection(arr1, arr2) { return arr1.filter(item => arr2.includes(item));
Array UnionProblem StatementWrite a function `getUnion(arr1, arr2)` that returns all unique elements from both arrays combined.Approach 1. Combine arrays using spread. 2. Use `new Set()` to remove duplicates. Solution`function getUnion(arr1, arr2) { return [...new Set([...arr1, ...arr2])];
Count OccurrencesProblem StatementWrite a function `countOccurrences(arr)` that returns an object with the frequency of each element.Approach 1. Use `.reduce()`. 2. For each item, increment its count in the accumulator object. Solution`function countOccurrences(arr) { return arr.reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc;
Most Frequent ItemProblem StatementWrite a function `mostFrequent(arr)` that returns the item that appears most often.Approach 1. Use frequency count logic. 2. Iterate through the frequency object to find the max. Solution`function mostFrequent(arr) { const counts = arr.reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc;
Get Last ElementProblem StatementWrite a function `getLast(arr)` that returns the last element without modifying the array.Approach 1. Access index at `arr.length - 1`. 2. Or use the modern `.at(-1)` method. Solution`function getLast(arr) { return arr.at(-1);
Create RangeProblem StatementWrite a function `createRange(start, end)` that returns an array with numbers from start to end (inclusive).Approach 1. Use `Array.from()` with a length property and a mapping function. Solution`function createRange(start, end) { const length = end - start + 1; return Array.from({ length