FAQ: Iterators - Review

Reduce is a common iteration pattern just like map and filter. It’s the pattern of combining values into an accumulated value, for example summing up numbers.
You’d probably make more sense of it if you implemented it. Just like map and filter, this is a very simple function. You could start by implementing map and filter since those are similar, you could also implement sum, and then generalize that to reduce.

1 Like

That’s how you can do it.

const myArray = [‘Albany’,‘britain’,‘dubai’,‘ukraine’,‘luxembourg’]

const myName = myArray.map(name => name[0]);


I have a question regarding the Mini Linter Project which i don’t know why i’m not able to open the forum topic for that but here it goes: We need to find how many “.” or “!” we have in order to count the sentences on the array. Of course the key for finding that is on the last character of each sentence. I got stuck and watched the walkthrough video and it appears to show that we do so checking of course the index of the sentence using .length on the parameter (sentences) , but i don’t understand why we need to specify “-1” next to “sentences.length” , why -1? hows does that work?

for (sentences of storyWords) {
  if (sentences[sentences.length -1] === '.' || sentences[sentences.length -1] === '!') {
    sentenceCount +=1;

thanks :slight_smile:

That is the notation for the last character of the sentence.

1 Like

Consider the difference between the length of a string, and the valid index values of a string. For example:

const myString = "Hello World!";
console.log(myString.length); //12
//is there a character at index 12?
console.log(myString[12]); //undefined

Since the valid index values start at 0 not 1, we have to subtract 1 from the length to get the index of the last character:

console.log(myString[myString.length - 1]); // !
1 Like

6 posts were split to a new topic: To use the optional index argument in the .map() method

Here is something i found for reduce

1 Like

Is ‘this’ and ‘thisArg’ something we’re going to learn about later, because they’re under ‘optional parameters’ in the MDN syntax for various methods. And I don’t know what they mean.

For the " * Chain two iteration methods on the same array." challenge i did this:

const array = [45, 55, 16, 36, 78, 45, 66, 22, 49];
const below50 = num => num < 50;
const reduceArray = (sum, add) => sum + add;
const newArray = array.filter(below50).reduce(reduceArray);

It seems working. Is it legit?
For me, the variant with array.reduce().filter() has more logic, but it is the “error” variant for computer.

1 Like

Hi! I wanted to challenge myself and try reducing a multi-dimensional array by returning the sum of all values. This is my solution:

const multiArray = [[1,2,3], [4,5,6], [7,8,9]];
const sumMultiArray = multiArray.reduce((accumulator, currentValue) => {
  currentValue = currentValue.reduce((accumulator, currentValue) => {
    return accumulator + currentValue;

  return currentValue + accumulator;
}, 0);


I found this quite a challenge and would love a review of my code. How could I improve? Are there any solutions that would have been more effective?

Thank you! :grinning: