Episode 1 — Fundamentals / 1.22 — Array Methods
1.22 — Exercise Questions: Array Methods
Practice questions for all ten subtopics in Section 1.22. Mix of short-answer, prediction, code-writing, and debugging tasks.
How to use this material (instructions)
- Read lessons in order —
README.md, then1.22.a→1.22.j. - Answer closed-book first — then compare to the matching lesson.
- Code each answer — type it out in a console or editor, do not just read.
- Interview prep —
1.22-Interview-Questions.md. - Quick review —
1.22-Quick-Revision.md.
1.22.a — map() (Q1–Q8)
Q1. What does map() return? Does it mutate the original array?
Q2. Predict the output:
const result = [1, 2, 3].map(n => { n * 2 });
console.log(result);
Q3. Write a map call that converts ["hello", "world"] to ["HELLO", "WORLD"].
Q4. Given const users = [{ name: "Alice", age: 25 }, { name: "Bob", age: 30 }], use map to create ["Alice (25)", "Bob (30)"].
Q5. Predict the output:
["1", "2", "3"].map(parseInt);
Why is this surprising? How do you fix it?
Q6. What is wrong with this code?
const pairs = [1, 2, 3].map(n => { value: n });
Q7. Write a map that adds a total property to each order: [{ item: "A", price: 10, qty: 3 }, ...].
Q8. Can map change the length of the resulting array? If you want fewer elements, what should you use?
1.22.b — filter() (Q9–Q16)
Q9. What does filter() return if no elements pass the test? Can it ever return null?
Q10. Write a filter call to get even numbers from [1, 2, 3, 4, 5, 6, 7, 8].
Q11. Predict the output:
const result = [0, 1, "", "hello", null, undefined, false, 42].filter(Boolean);
console.log(result);
Q12. Given an array of products, write a filter to select items with price < 50 AND inStock === true.
Q13. Write a one-liner to remove duplicates from [1, 2, 2, 3, 3, 3, 4] using filter.
Q14. Why should you generally filter before map in a chain?
Q15. Write a search function that filters an array of { name, email } objects by a query string (case-insensitive, matches against both name and email).
Q16. What is the difference between filter(x => x) and filter(Boolean)?
1.22.c — reduce() (Q17–Q26)
Q17. What happens when you call reduce on an empty array without an initial value?
Q18. Write a reduce to sum all numbers in [10, 20, 30, 40]. Show the step-by-step trace.
Q19. Write a reduce to find the maximum value in [3, 7, 2, 9, 5].
Q20. Write a reduce to count the frequency of each character in "hello world" (ignoring spaces).
Q21. Write a reduce that groups this array by category:
[{ name: "A", category: "x" }, { name: "B", category: "y" }, { name: "C", category: "x" }]
Q22. What is wrong with this reduce?
const sum = [1, 2, 3].reduce((acc, n) => {
if (n > 1) acc + n;
return acc;
});
Q23. Explain why reduce is called the "universal" array method.
Q24. Write a reduce that flattens [[1, 2], [3, 4], [5, 6]] into [1, 2, 3, 4, 5, 6].
Q25. What is reduceRight? Give one use case.
Q26. Write a reduce to convert [["name", "Alice"], ["age", "25"], ["city", "NYC"]] into { name: "Alice", age: "25", city: "NYC" }.
1.22.d — forEach() (Q27–Q33)
Q27. What does forEach return?
Q28. Can you break out of a forEach loop? What about return?
Q29. What is wrong with this code?
const urls = ["/api/1", "/api/2"];
urls.forEach(async (url) => {
const data = await fetch(url).then(r => r.json());
console.log(data);
});
console.log("All done!");
Q30. A junior developer wrote const names = users.forEach(u => u.name). What is names?
Q31. When should you use forEach instead of map?
Q32. Rewrite this forEach as a map:
const doubled = [];
[1, 2, 3].forEach(n => doubled.push(n * 2));
Q33. What loop construct should you use if you need await inside a loop?
1.22.e — some() and every() (Q34–Q40)
Q34. What does [].some(x => true) return? What about [].every(x => false)?
Q35. Predict the output:
const nums = [2, 4, 5, 6, 8];
console.log(nums.every(n => n % 2 === 0));
console.log(nums.some(n => n % 2 !== 0));
Q36. Write a some call to check if any user in an array has role === "admin".
Q37. Write an every call to check if all fields in a form object have non-empty values.
Q38. Why is arr.some(fn) more efficient than arr.filter(fn).length > 0?
Q39. Write a one-liner to check if an array has any duplicates.
Q40. Explain what "short-circuit evaluation" means in the context of some and every.
1.22.f — slice() and splice() (Q41–Q48)
Q41. Does slice mutate the original array? Does splice?
Q42. Predict the output:
const arr = [10, 20, 30, 40, 50];
console.log(arr.slice(1, 3));
console.log(arr.slice(-2));
console.log(arr);
Q43. Predict the output:
const arr = [10, 20, 30, 40, 50];
const removed = arr.splice(1, 2);
console.log(removed);
console.log(arr);
Q44. Write a splice call that inserts "x" at index 2 without removing any elements.
Q45. How do you remove an element at index 3 from an array without mutating the original?
Q46. What does arr.slice() (no arguments) do?
Q47. Write a pagination function using slice: given an array, page number, and page size, return the correct page.
Q48. What does splice return?
1.22.g — sort() (Q49–Q55)
Q49. Predict the output:
console.log([10, 2, 1, 20, 3].sort());
Why is this result surprising?
Q50. Write a compare function to sort numbers in ascending order.
Q51. Write a compare function to sort objects by a name property alphabetically (case-insensitive).
Q52. Does sort() return a new array or the same array?
Q53. What is toSorted() and why would you prefer it over sort()?
Q54. Write a multi-level sort: sort by age ascending, then by name alphabetically as a tiebreaker.
Q55. What does "stable sort" mean, and since what ES version is it guaranteed?
1.22.h — flat() and flatMap() (Q56–Q61)
Q56. Predict the output:
console.log([1, [2, [3, [4]]]].flat());
console.log([1, [2, [3, [4]]]].flat(Infinity));
Q57. What is the default depth for flat()?
Q58. How does flatMap differ from calling .map().flat()?
Q59. Write a flatMap that takes ["hello world", "foo bar"] and returns ["hello", "world", "foo", "bar"].
Q60. Use flatMap to double each number: [1, 2, 3] becomes [1, 1, 2, 2, 3, 3].
Q61. Does flat() remove holes in sparse arrays?
1.22.i — Functional Thinking (Q62–Q67)
Q62. What is the difference between imperative and declarative code?
Q63. Name three non-mutating array methods and three mutating ones.
Q64. Why should you generally filter before map in a chain?
Q65. When is a for loop preferable to a functional chain?
Q66. Rewrite this imperative code using array methods:
const result = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 10) {
result.push(nums[i] * 2);
}
}
Q67. What is the danger of using sort() inside a function that receives an array parameter?
Comprehensive / Cross-topic (Q68–Q80)
Q68. Write a chain that takes an array of students, filters those with grade > 80, maps to names, and sorts alphabetically.
Q69. Given an array of transactions ({ type: "income"|"expense", amount }), calculate net income using reduce.
Q70. Write a pipeline that takes raw API user data, filters active users, transforms to { id, displayName }, and sorts by displayName.
Q71. What is the output?
const arr = [1, 2, 3];
const result = arr.map(n => n * 2).filter(n => n > 3).reduce((a, b) => a + b, 0);
console.log(result);
Q72. Implement Array.prototype.includes using some.
Q73. Write a function that takes an array of numbers and returns { min, max, sum, avg } using reduce.
Q74. Given nested arrays of tags, flatten them and return a sorted list of unique tags.
Q75. Write a one-liner to check if all strings in ["hello", "world", "foo"] have length >= 3.
Q76. What is the output?
const arr = [1, 2, 3, 4, 5];
console.log(arr.slice(1, -1));
console.log(arr.filter(n => n !== 3));
console.log(arr);
Q77. Why does [10, 9, 8].sort() produce [10, 8, 9]?
Q78. Write a reduce that implements flatMap functionality for [[1, 2], [3], [4, 5]].
Q79. What is the difference between find and filter? When would you use each?
Q80. Write a function that takes an array of objects and a key, and returns the objects grouped by that key (generic group-by).
Answer hints
| Q | Hint |
|---|---|
| Q2 | Missing return — curly braces need explicit return |
| Q5 | parseInt(element, index) — index is used as radix |
| Q11 | filter(Boolean) removes all falsy values |
| Q17 | TypeError: Reduce of empty array with no initial value |
| Q27 | undefined — always |
| Q34 | some([]) = false, every([]) = true (vacuous truth) |
| Q43 | removed = [20, 30], arr = [10, 40, 50] |
| Q49 | Lexicographic: [1, 10, 2, 20, 3] |
| Q71 | map: [2, 4, 6] → filter: [4, 6] → reduce: 10 |
| Q77 | Default sort converts to strings: "10" < "8" < "9" |
← Back to 1.22 — Array Methods (README)