FAQ: Iterators - Introduction to Iterators

This community-built FAQ covers the “Introduction to Iterators” exercise from the lesson “Iterators”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Introduction to Iterators

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

How you do to make this function display the names inside the array randomly?

const artists = [‘Picasso’, ‘Kahlo’, ‘Matisse’, ‘Utamaro’, ‘Byakes’];

artists.forEach(artist => {
console.log(artist + ’ is one of my favorite artists.’);
});

//Try this instead

const artists = [‘Picasso’, ‘Kahlo’, ‘Matisse’, ‘Utamaro’];

// This will generate a random index in the artists array
const randomIndex =artist=> {
return Math.floor((Math.random() * artists.length));
}
//Each time we log artists array with a random index, it will give us a random artists (plus string interpolation)
console.log(${artists[randomIndex()]} is one of my favorite artists.);

1 Like

Does an iterator for an array method HAVE to be a callback function? Or will any kind of code work? (assuming it’s a working piece of code).

In previous lessons you breifly went over arrow functions as a footnote, and only ever had us use them if we wanted to, depending on our preferred way of writing functions. Now this assignment is forcing us to use arrow functions and expects us to be proficient at them. I would’ve liked WAY more practice using arrow functions beforehand. This is where a khanacademy approach of drilling a concept before moving on would be nice.

2 Likes

what does .map method do?

Mapping is a one-to-one operation. The output array contains one element mapped from the corresponding element in the input array, applying the same operation to all input elements.

arr1 = [2, 4, 8, 16, 32]

arr2 = arr1.map(function (x) { return x ** 2 })

console.log(arr2)

// [ 4, 16, 64, 256, 1024 ]

The function inside the argument is known as the callback function. We can simplify it using ES6 arrow syntax…

arr2 = arr1.map(x => x ** 2)

Note that the function performs the operation on the input elements, each in turn, then appends the value to the output array at the same index as the input value.

1 Like

I dont understand the .filter() section of the code. Why is it outputting numbers? The function is saying to “return typeof thing === ‘number’;”. In my mind that saying return the exact value of the string ‘number’. Does ‘number’ (with quotation) mean actual numbers?

The above return statement would be from a predicate function which only returns a boolean.

filter iterates over the object we invoke it upon, and returns a new array of values that meet with (satisfy) the predicate.

In the OP example, the predicate asserts that only values that are actual numbers will pass through the filter and be in the resulting array.


Extra study

When you reach the includes iterator and have it down, come back to this…

let fizz = [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
let buzz = [5, 10, 15, 20, 25, 30]
let fizzbuzz = fizz.filter(x => buzz.includes(x))
console.log(fizzbuzz)
// [15, 30]
//.forEach + Math.random
const fruits = ['mango', 'papaya', 'pineapple', 'apple'];
let r = fruits[Math.floor(Math.random()*fruits.length)];
fruits.forEach(eat => {
  	console.log(`I want to eat a ${eat} or ${r}`)
    if (r=== eat)          {
      console.log(`hmm, it's kinda awkward, but I'm not gonna eat 2 ${r} o_O`)
    }
 });

results:

‘I want to eat a mango or pineapple’

‘I want to eat a papaya or pineapple’

‘I want to eat a pineapple or pineapple’

‘hmm, it’s kinda awkward, but I’m not gonna eat 2 pineapple o_O’

‘I want to eat a apple or pineapple’

Hello, in bit43…'s question they say “return typeof thing === ‘number’;” - that may mean that in js the ‘number’ element/argument is a given that it means a boolean type of object? A predicate function is a standard js rule that if you ask " === ‘number’ " it asks if it is a boolean?What if your array’s element is indeed ‘number’?

number is the value returned from typeof object when object is numerical data.

The result of a comparison is boolean.

A predicate function returns a boolean, but does not necessarily ask if any values are boolean. In the case above, it will return true if the identity comparison is the same, and false otherwise.