Paul Jacobson
Paul Jacobson

Reputation: 85

Using JavaScript, how do I combine items in two arrays parsed with forEach() into a single string?

I'm working on an exercise that I can't find a solution for. This is the task:

Create an array to hold your top choices (colors, presidents, whatever).

  1. For each choice, log to the screen a string like: "My #1 choice is blue."

  2. Change it to log "My 1st choice, "My 2nd choice", "My 3rd choice", picking the right suffix for the number based on what it is.

I have completed the first part of the task with the following:

const list = ['blue', 'green', 'yellow', 'red'];
const prefs = ['first', 'second', 'third', 'fourth']; // For the second part

list.forEach((items, index, array) => {
  console.log(`My #1 choice is ${items} at position ${index} in the list that contains: ${array}`); // I included additional parameters to better understand the forEach() method
});

I can't work out how to complete the second part of the exercise that basically involves creating a string that links list[0] with prefs[0], and iterates.

I saw a possible, pre-ES6 solution for this that uses a really verbose if loop. What is an effective way to create the string required for the second part of the exercise? There must be a better solution than the one I saw?

Upvotes: 0

Views: 112

Answers (4)

Chris Geirman
Chris Geirman

Reputation: 9684

Here's a robust solution that will handle any number properly...

const list = ['blue', 'green', 'yellow', 'red', 'purple']; const prefs = ['st', 'nd', 'rd']; // For the second part

const getNumWithSuffix = (i) => {
    var j = i % 10,
        k = i % 100;
    if (j == 1 && k != 11) {
        return i + "st";
    }
    if (j == 2 && k != 12) {
        return i + "nd";
    }
    if (j == 3 && k != 13) {
        return i + "rd";
    }
    return i + "th";  

}


list.forEach((items, index, array) => {
  console.log(`My ${getNumWithSuffix(index+1)} choice is ${items}.`);
});

Outputs...

My 1st choice is blue.
My 2nd choice is green.
My 3rd choice is yellow.
My 4th choice is red.
My 5th choice is purple.

Upvotes: 0

Momo
Momo

Reputation: 482

Since you have index of list array, you can easily call prefs array with its index

const list = ['blue', 'green', 'yellow', 'red'];
const prefs = ['first', 'second', 'third', 'fourth']; // For the second part

list.forEach((items, index, array) => {
    console.log(`My #${index+1} choice is ${items}`); // first part
    console.log(`My ${prefs[index]} choice is ${items}`); // second part
});

Alternatively, to ensure you have the same length of array, you can change it to two-dimensional array

const list2 = [
    ['first', 'blue'],
    ['second', 'green'],
    ['third', 'yellow'],
    ['fourth', 'red'],
];

list2.forEach((items, index, array) => {
    console.log(`My #${index+1} choice is ${items[1]}`); // first part
    console.log(`My ${items[0]} choice is ${items[1]}`); // second part
});

Upvotes: 1

maggiekh
maggiekh

Reputation: 204

The second part of the exercise hints at what you should be doing. Notice that the prefs there are 1st, 2nd, etc and not "first" or "second".

The solution you found is the easiest way, I think, besides your solution of mapping out all of the possibilities. English isn't very nice in that regard - though realistically you only have to map out 1-12 and then you can calculate through there. 21st = "twenty" + "first".

However, just checking what the ones digit is and doing

if((index+1) % 10 === 1){
  console.log(`my ${index+1}st choice is ${array[index]}`
}

and so on (or whatever) really is your best bet. If you want it to look nicer you can make a function to return the ordinal.

Upvotes: 0

Sam Creamer
Sam Creamer

Reputation: 5361

Make sure your lists are the same length:

list.forEach((items, index, array) => {
  console.log(`My ${prefs[index]} choice is ${items}.`);
});

Upvotes: 1

Related Questions