FOR LOOPS and arrays

FOR LOOPS and arrays


Guys I need some help in this:

Create a function theBeatlesPlay, which accepts two parameters- an array of musicians and an array of instruments. The body of the function should create an empty array stored in a variable. The function should also contain a for loop which loops over the array of musicians. You’ll want to be careful about what value you set your counter variable to store. (Hint: Think about what the first index of an array is). The first time through the loop, the body of the loop should create a string using the first index of the musicians array and the first index of the instruments array: “John Lennon plays guitar”. This string should be added to the empty array you created. The loop should make the same sentence for every member of the musicians array. The function should return the array of new strings.


I have done this so far ;:

function theBeatlesPlay(musicians,instruments) {

var musician = [“John Lennon”, “Paul McCartney”, “George Harrison”, “Ringo Starr”]
var instrument = [“Guitar”, “Bass Guitar”, “Lead Guitar”, “Drums”];



Those arrays aren’t supposed to be passed along with the arguments?

You just need to declare a variable and initialize it with an empty array. Then a for loop to iterate over each element of one of the arrays in the arguments (assuming both have the same length, of course). Inside the for loop, create a string interpolating the values for the current index on both arrays and push the string to the empty array. Finally, return the array.

It’s easy to code, but if I code it for you I don’t think you will learn anything along the way :slight_smile:

Good luck!


may i include the var musician and instrument as well?


What do you mean? You are already including it in the scope of the function by passing them as arguments, if that’s what you mean.






Well, musicians and instruments are already being passed as arguments in the function. The arrays with the musicians and instruments are being passed through the arguments, from outside of the function.

Ok. So first you need to declare the function:

function theBeatlesPlay( musicians, instruments ) {

Then, you’re being asked to create an empty array. Let’s call it just arr:

let arr = [ ];

Now you need to loop through every musician in the musicians array, make a string like “John Lennon plays guitar” and push it to the array from the previous line. Please, try hard to do it on your own and, if you’re still stuck, click on the blurred image to see how to do it.

for ( let i=0; i < musicians.length; i++ ) {
  const str = musicians[ i ] + ' plays ' + instruments[ i ];
  arr.push( str );

Finally, after the loop, you need to return the array with all the strings created.

return arr;

And that’s it. You just need to call the function if you want to see that it works as expected. You can assign it to a variable and then print it to the console to check if everything prints out correctly, like this:

const whoPlaysWhat = theBeatlesPlay(
  [ 'John Lennon', 'Paul McCartney', 'George Harrison', 'Ringo Starr' ],
  [ 'Guitar', 'Bass Guitar', 'Lead Guitar', 'Drums' ]

console.log( whoPlaysWhat );

As you can see, the arrays with all the musicians and intruments are being passed through the arguments, so you don’t need to create those inside the scope of your function.

Make sense? Please, try it yourself before seeing my solution for the loop.



like this?


Exactly. That returns an array with all the strings.