0

As part of my learning exercise, I am trying to create a function with a For loop. The function returns a new array variable merges and matches the index value of both array musicians and instruments. So the expected return should be string: "John Lennon Plays Guitar"

Here is my attempt, but I keep failing to get the return:

var musicians = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]
var instruments = ["Guitar", "Bass Guitar", "Lead Guitar", "Drums"]

  function theBeatlesPlay(musicians, instruments) {      

    for (let i=0; i<musicians.length; i++) {

      var newMusicians = musicians[i]
      var newInstruments = instruments[i]
    }

    console.log(newMusicians[i] + "plays" + instruments[i])
  }
2
  • console.log should be inside the for loop Commented May 25, 2017 at 5:17
  • Put the console.log inside the for loop. Also, there's no need for those variables. Commented May 25, 2017 at 5:17

4 Answers 4

1

You're overwriting your variables on every iteration of the loop. You need to create an empty array and fill it (and then return it):

var musicians = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]
var instruments = ["Guitar", "Bass Guitar", "Lead Guitar", "Drums"]

function theBeatlesPlay(musicians, instruments) {
  var combinations = [];

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

  return combinations;
}

console.log(theBeatlesPlay(musicians, instruments));

Sign up to request clarification or add additional context in comments.

Comments

1

Couple of issues are there.

The function need to return the merged array. Secondly the function was never called. You may not need to again create a variable inside the loop

var musicians = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]
var instruments = ["Guitar", "Bass Guitar", "Lead Guitar", "Drums"]


function theBeatlesPlay(musicians, instruments) {
  var mergedArray = [];

  for (let i = 0; i < musicians.length; i++) {
    mergedArray.push(musicians[i] + " plays " + instruments[i])

  }
  return mergedArray;

}
console.log(theBeatlesPlay(musicians, instruments))

Comments

0

Few observations as per your code :

  • Add ; after each statement inside for loop for good coding practices.
  • put console.log statement inside for loop as it will print the values iterates inside for loop.
  • Instead of newMusicians[i] use newMusicians in console.log.

var musicians = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]
var instruments = ["Guitar", "Bass Guitar", "Lead Guitar", "Drums"]

  function theBeatlesPlay(musicians, instruments) {      

    for (let i = 0; i < musicians.length; i++) {
      var newMusicians = musicians[i];
      var newInstruments = instruments[i];
      console.log(newMusicians + " plays " + instruments[i]);
    }
  }
  
 theBeatlesPlay(musicians, instruments); 

Comments

-1
  1. You need to put console.log inside the for loop.

  2. Also, console.log(newMusicians + " plays " + instruments[i])

newMusicians instead of newMusicians[i] in while logging

var musicians = ["John Lennon", "Paul McCartney", "George Harrison", "Ringo Starr"]
var instruments = ["Guitar", "Bass Guitar", "Lead Guitar", "Drums"]

  function theBeatlesPlay(musicians, instruments) {      

    for (let i=0; i<musicians.length; i++) {

      var newMusicians = musicians[i];
      var newInstruments = instruments[i];
      console.log(newMusicians + " plays " + instruments[i])
    }

    
  }
  
  theBeatlesPlay(musicians, instruments);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.