Skip to content

Latest commit

 

History

History
353 lines (242 loc) · 5.54 KB

File metadata and controls

353 lines (242 loc) · 5.54 KB

Section 16 — Array Methods


map()

Q212. Square Each Number

Difficulty: Easy

Given an array of numbers, use map() to return a new array where each element is the square of the original.

Example:

Input:  [1, 2, 3, 4]
Output: [1, 4, 9, 16]

Q213. Convert to Uppercase

Difficulty: Easy

Given an array of strings, use map() to return a new array where every string is uppercase.

Example:

Input:  ["hello", "world"]
Output: ["HELLO", "WORLD"]

Q214. Add 10 to Each Element

Difficulty: Easy

Given an array of numbers, use map() to return a new array with 10 added to each element.

Example:

Input:  [1, 5, 10]
Output: [11, 15, 20]

Q215. Extract Names from Objects

Difficulty: Easy

Given an array of person objects [{ name: "Alice", age: 25 }, ...], use map() to extract and return just the names.

Example:

Input:  [{ name: "Alice", age: 25 }, { name: "Bob", age: 30 }]
Output: ["Alice", "Bob"]

Q216. Convert Celsius to Fahrenheit

Difficulty: Easy

Given an array of temperatures in Celsius, use map() to convert each to Fahrenheit.

Example:

Input:  [0, 100, 37]
Output: [32, 212, 98.6]

filter()

Q217. Even Numbers

Difficulty: Easy

Given an array of numbers, use filter() to return only the even numbers.

Example:

Input:  [1, 2, 3, 4, 5, 6]
Output: [2, 4, 6]

Q218. Odd Numbers

Difficulty: Easy

Given an array of numbers, use filter() to return only the odd numbers.

Example:

Input:  [1, 2, 3, 4, 5]
Output: [1, 3, 5]

Q219. Positive Numbers

Difficulty: Easy

Given an array of numbers (including negatives), use filter() to return only the positive numbers.

Example:

Input:  [-3, 0, 1, -1, 5, 7]
Output: [1, 5, 7]

Q220. Prime Numbers

Difficulty: Easy

Given an array of numbers, use filter() to return only the prime numbers.

Example:

Input:  [1, 2, 3, 4, 5, 6, 7]
Output: [2, 3, 5, 7]

Q221. Strings Longer Than 5 Characters

Difficulty: Easy

Given an array of strings, use filter() to return only strings whose length is greater than 5.

Example:

Input:  ["hi", "hello", "javascript", "js"]
Output: ["hello", "javascript"]

reduce()

Q222. Sum Array

Difficulty: Easy

Given an array of numbers, use reduce() to compute the total sum.

Example:

Input:  [1, 2, 3, 4, 5]
Output: 15

Q223. Product Array

Difficulty: Easy

Given an array of numbers, use reduce() to compute the product of all elements.

Example:

Input:  [1, 2, 3, 4]
Output: 24

Q224. Maximum Element

Difficulty: Easy

Given an array of numbers, use reduce() to find the maximum element without using Math.max.

Example:

Input:  [3, 7, 1, 9, 4]
Output: 9

Q225. Minimum Element

Difficulty: Easy

Given an array of numbers, use reduce() to find the minimum element.

Example:

Input:  [3, 7, 1, 9, 4]
Output: 1

Q226. Frequency Map

Difficulty: Easy

Given an array of values, use reduce() to build an object mapping each unique value to its frequency.

Example:

Input:  ["a", "b", "a", "c", "b", "a"]
Output: { a: 3, b: 2, c: 1 }

Q227. Flatten Array

Difficulty: Medium

Given a nested array (one level deep), use reduce() to flatten it into a single array.

Example:

Input:  [[1, 2], [3, 4], [5]]
Output: [1, 2, 3, 4, 5]

Q228. Group Words by Length

Difficulty: Medium

Given an array of words, use reduce() to group them into an object keyed by word length.

Example:

Input:  ["hi", "hey", "bye", "go", "ok"]
Output: { 2: ["hi", "go", "ok"], 3: ["hey", "bye"] }

some()

Q229. Any Negative Number?

Difficulty: Easy

Given an array of numbers, use some() to check if any element is negative. Return true or false.

Example:

Input:  [1, 2, -3, 4]
Output: true

Q230. Any Prime Number?

Difficulty: Easy

Given an array of numbers, use some() to check if any element is prime.

Example:

Input:  [4, 6, 7, 9]
Output: true

Q231. Any Duplicate?

Difficulty: Easy

Given an array, use some() to check if any element appears more than once.

Example:

Input:  [1, 2, 3, 2]
Output: true

every()

Q232. All Positive?

Difficulty: Easy

Given an array, use every() to check if all elements are positive numbers.

Example:

Input:  [1, 3, 5, 7]   →  true
Input:  [1, -2, 5]     →  false

Q233. All Even?

Difficulty: Easy

Given an array, use every() to check if all elements are even.

Example:

Input:  [2, 4, 6]  →  true
Input:  [2, 3, 6]  →  false

Q234. All Strings?

Difficulty: Easy

Given an array, use every() to check if every element is of type string.

Example:

Input:  ["hello", "world"]  →  true
Input:  ["hello", 42]       →  false

find()

Q235. First Even

Difficulty: Easy

Given an array, use find() to return the first even number. Return undefined if none.

Example:

Input:  [1, 3, 4, 6]
Output: 4

Q236. First Negative

Difficulty: Easy

Given an array, use find() to return the first negative number.

Example:

Input:  [5, 3, -1, -4, 2]
Output: -1

Q237. First Palindrome

Difficulty: Easy

Given an array of strings, use find() to return the first string that is a palindrome.

Example:

Input:  ["hello", "racecar", "level"]
Output: "racecar"