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:

1 Like

const ail = [‘you’, ‘orléans’, ‘uganda’]
const ordi = ail.reduce(accumulator, currentValue) => {
return accumulator[0] + currentValue[0]
console.log(I love ${ordi})

this code was supposed to give ‘I love you’, but it returned ‘I love yu’.
where’s my mistake?

In step 4 of the Mini Linter Project, I get the accurate results of counting the # of overused words with the following code (sorry, I reproduced the whole project code block but my question pertains to the last step shown at the end of this reproduced block) : If put a “return” into the curly braces just before resetting any one of the 3 defined variables (that is resetting the variable reallyCount = reallyCount+ 1 , for example as shown in the code), then I get nothing back on the console.logs at the end of this code. Which brings me to my confusion: when creating a callback function, when do you need “return” and when don’t you?

let story = ‘Last weekend, I took literally the most beautiful bike ride of my life. The route is called “The 9W to Nyack” and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It’s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.’;

let overusedWords = [‘really’, ‘very’, ‘basically’];

let unnecessaryWords = [‘extremely’, ‘literally’, ‘actually’ ];

const storyWords = story.split (’ ')
const betterWords = storyWords.filter(function(word) { return !unnecessaryWords.includes(word)
let reallyCount = 0
let veryCount = 0
let basicallyCount = 0

for (i=0; i<betterWords.length; i++)
{if (betterWords[i]===‘really’) { reallyCount = reallyCount+1;}
else if (betterWords[i]===‘very’) {
veryCount = veryCount+1;}
else if (betterWords[i]===‘basically’) {
basicallyCount = basicallyCount + 1}

I an trying to do step # 8 on the Mini Linter Project - kind of like “extra credit” because there’s no solution to look if you’re stuck and the narrated solution by the professional developer stops at step 7. The first of 3 challenges in step 8 says to remove every other of each of the “overused words” (there are three overused words in an array and I’m first trying to remove every other word “really”), Below is what I’ve coded which has errors which I can’t decipher (beginning with I don’t know if the “for” function can even be used as a callback function but its what I tried). Since there’s no guidance in the project, I’m baring my soul here with my stab at the first part of this step 8 .Here’s my code (note the “betterWords” array referred to below was previously established in a step prior to step 8) :

let lessOverusedWords = betterWords.filter (for (word2=0; word2<betterWords.length; Word2++) {if (betterWords[word2] === ‘basically’ && word2 %2 =0) return lessOverusedWords = betterWords.splice[word2]})