JavaScript Exercises: Mastering Array map()
Method
These exercises are designed to help you master the map()
method, which creates a new array populated with the results of calling a provided function on every element in the calling array.
Exercises
1. Square Numbers
Question: Write a function squareNumbers
that uses map()
to square each number in an array.
Required Output for [1, 2, 3]
: [1, 4, 9]
Required Output for [1, 2, 3, 4]
: [1, 4, 9, 16]
2. Convert to Boolean
Question: Write a function convertToBoolean
that converts each element in an array to its Boolean equivalent.
Required Output for [0, 1, 2, '']
: [false, true, true, false]
3. Append Currency
Question: Write a function appendCurrency
that appends ‘$’ to each number in an array.
Required Output for [1, 2, 3]
: ['$1', '$2', '$3']
Required Output for [1, 5, 10, 100]
: ['$1', '$5', '$10', '$100']
4. Extract Names
Question: Write a function extractNames
that extracts names from an array of objects.
Required Output for [{ name: 'Alice' }, { name: 'Bob' }]
: ['Alice', 'Bob']
5. Calculate Lengths
Question: Write a function calculateLengths
that calculates the length of each string in an array.
Required Output for ["hello", "world"]
: [5, 5]
6. Increment by Index
Question: Write a function incrementByIndex
that increases each array element by its index value.
Required Output for [0, 0, 0, 0]
: [0, 1, 2, 3]
7. Parse to Integer
Question: Write a function parseToInt
that converts each string number in an array to an integer.
Required Output for ["1", "2", "3"]
: [1, 2, 3]
8. Multiply by Factor
Question: Write a function multiplyByFactor
that multiplies each element in an array by a specified factor.
Required Output for ([1, 2, 3], 2)
: [2, 4, 6]
9. Normalize Strings
Question: Write a function normalizeStrings
that converts all strings in an array to lowercase.
Required Output for ["Hello", "WORLD"]
: ["hello", "world"]
10. Index as Key
Question: Write a function indexAsKey
that creates an object from each element with the index as the key.
Required Output for ["apple", "banana"]
: [{"0": "apple"}, {"1": "banana"}]
11. Convert to Binary
Question: Write a function convertToBinary
that converts each number in an array to its binary string.
Required Output for [5, 7, 10]
: ["101", "111", "1010"]
12. Double Odd Numbers
Question: Write a function doubleOdds
that doubles only the odd numbers in an array.
Required Output for [1, 2, 3, 4]
: [2, 2, 6, 4]
13. Extract IDs
Question: Write a function extractIDs
that extracts IDs from an array of objects.
Required Output for [{ id: 1 }, { id: 2 }]
: [1, 2]
14. Negate Numbers
Question: Write a function negateNumbers
that negates each number in an array.
Required Output for [1, -2, 3]
: [-1, 2, -3]
15. Prefix Strings
Question: Write a function prefixStrings
that adds ‘ID-’ before each element in an array.
Required Output for ["123", "456"]
: ["ID-123", "ID-456"]
16. Unpack Objects
Question: Write a function unpackObjects
that unpacks an array of objects into an array of values based on a provided key.
Required Output for ([{ key: 'value1' }, { key: 'value2' }], 'key')
: ['value1', 'value2']
17. Round Numbers
Question: Write a function roundNumbers
that rounds each number in an array to the nearest integer.
Required Output for [1.5, 2.3, 2.8]
: [2, 2, 3]
18. Absolute Values
Question: Write a function absoluteValues
that converts each number in an array to its absolute value.
Required Output for [-1, -5, 2, 3]
: [1, 5, 2, 3]
19. Deconstruct Email
Question: Write a function deconstructEmail
that extracts the username from each email in an array.
Required Output for ["alice@example.com", "bob@sample.com"]
: ["alice", "bob"]
20. Percentage Format
Question: Write a function percentageFormat
that converts each decimal number in an array to a percentage format string.
Required Output for [0.1, 0.23, 0.5]
: ["10%", "23%", "50%"]
Conclusion
By practicing these exercises, you can improve your understanding of the map
method in JavaScript. You can also use JavaScript arrays and objects to manipulate data efficiently.