Guide
Video
Resources
Sections:

Introduction

First-class Functions

pluralize

map

forEach

filterArray

eitherFilter

eitherCallback

reduce

intersection

union

objOfMatches

arrToObj

multiMap

majority

prioritize

countBy

groupBy

goodKeys

Conclusion

Sections:

pluralize

Challenge Overview

The first challenge in the Callbacks and Higher Order functions module is called pluralize, and the prompt is as follows:
Create a function pluralize that takes an array of strings as input and returns a new array with an "s" added to the end of each string in the input array. For example, if the string "carrot" is in the input array, it should become the string "carrots" in the output array.

The body of the pluralize function should employ a single for loop that pluralizes each string in the input array.

Example:
Input
array = ["dog", "cat", "tree frog"]
Output
map(array) === ["dogs", "cats", "tree frogs"]
This challenge’s prompt is very straightforward and we won’t be using any higher order functions to solve it. While it may seem basic, it’s still an important starting point because we’ll be applying the process of deriving a solution to all of the remaining challenges as well. Let’s start by breaking down some of the language used in this prompt so we can see what clues it gives us in terms of how to write a function that solves it.

Solution Walkthrough

First, we’ll define our input and output. It’s clear that the input is an array. The important part here is that our function “returns a new array”; we’re returning something that did not previously exist, so we’ll have to initialize it.

The next key phrase is that this function is making a change to “each” string in the input array. Anytime we’re doing something to some or all of the elements in a collection, we’ll generally need some form of iteration to accomplish this.

Lastly, we see that before we can push each element of the input into our output array, we’ll need to add another string to it: 's'. This will involve string concatenation. We'll then add each concatenated string to our output array, and return it after we're done.

Putting this all together, here's the pseudocode for our function:

pluralize(inputArr):
initialize empty array outputArr
for each string of inputArr
    concatenate string with 's'
    push concatenated string into outputArr
return outputArr

Let's go through this pseudocode step by step, using the example array from CSX as our input.

1 / 10
When our function is invoked, it will first import the passed-in array as the value of inputArr.
2 / 10
We'll start by initializing the array that we'll be returning, outputArr.
3 / 10
Next, we'll iterate through the input array. We will enter the scope of this for loop's block. string represents the value of the first element of the inputArr, 'dog'.
4 / 10
We'll add 's' to string, getting 'dogs'.
5 / 10
Now, we'll add the concatenated string to outputArr, which is in the parent scope.

We've reached the end of this block, so we enter the next iteration of the for loop. string represents the value of the next element of the inputArr, 'cat'.
6 / 10
We'll add 's' to string, getting 'cats'.
7 / 10
We will once again add the concatenated string to outputArr.

We've reached the end of this for loop block once more, so we enter the next iteration of the for loop. The variable string receives the value of the next and final element of the inputArr, 'tree frog'.
8 / 10
We'll add 's' to string, getting 'tree frogs'.
9 / 10
Now, we'll add this final concatenated string to outputArr in the parent scope.

We've reached the end of the for loop block, and there are no more inputArr elements to iterate through. We return the parent scope and move to the next line after this block.
10 / 10
We can now return the output array, which contains all of the appropriately concatenated strings.

Coding Demonstration

const pluralize = (inputArr) => {
  const outputArr = [];
  
  for (let i = 0; i < inputArr.length; i += 1) {
      outputArr.push(`${inputArr[i]}s`);
  }
  
  return outputArr;
};

const animals = ['dog', 'cat', 'tree frog'];
console.log(pluralize(animals)); // -> ['dogs', 'cats', 'tree frogs']