JavaScript Iterator Lab
Medium
8
1
48.5% Acceptance
In this lab, you'll develop a deeper understanding of iterators in JavaScript by creating two fundamental functions: range
and mapIterator
. These functions are essential for array manipulation, allowing you to generate and transform arrays effectively. Remember to export each function using export default
so they can be imported and utilized in other files.
Challenges
-
Creating and Exporting the Range Function:
- Task: Write a function named
range
that takes two arguments,start
(inclusive) andend
(inclusive), and returns an array of numbers ranging fromstart
toend
. - Key Logic: Initialize an empty array, use a loop to add numbers from
start
toend
to this array, and then return the array. - Export: Ensure to export this function as the default export from its file.
- Examples to Test:
range(1, 3)
should return[1, 2, 3]
.range(5, 8)
should return[5, 6, 7, 8]
.
- Task: Write a function named
-
Creating and Exporting the MapIterator Function:
- Task: Develop a function named
mapIterator
that takes an array and a callback function as arguments, returning a new array with elements transformed by the callback. - Key Logic: Start with an empty array, iterate over the input array, apply the callback function to each element, and add the result to the new array.
- Export: Export this function as the default export from its file.
- Examples to Test:
- Using
mapIterator
on[1, 2, 3]
with a callback that doubles the number should return[2, 4, 6]
. - Applying
mapIterator
to[2, 4, 6]
with a callback that adds 5 to each number should result in[7, 9, 11]
.
- Using
- Task: Develop a function named
-
Applying the created Range and MapIterator Functions
- Import both
range
andmapIterator
functions in theindex.js
file - Create and export a variabe named
myRange
that holds the value ofrange(1,10)
- Create a function
timesTwo
that takes in a valuen
and returns2n
- Create and export a variable named
doubledRange
which stores the return value ofmapIterator
with the argumentsmyRange
andtimesTwo
All the best!