0

I was curious how I'll be able to return two arrays within a function. For example lets say we got an array of countries and a list of todo.

var countries = [
 "Japan",
 "USA"
]

var todo = [
 "go to the park",
 "drink amazing whiskey"
]

The way I wish the answer to come out is after it's looped we'll see

"Japan I will go to the park"
"USA I will drink amazing whiskey"

The way I wrote my function at the moment was:

function myTrip(countries, todo){
  var newArr = [];

  for (var i = 0; i < countries.length; i++) {
   newArr += countries[i]
  }
  for (var i = 0; i < todo.length; i++) {
   newArr += todo[i]
  }
  return newArr
}

I honestly don't know where to go from here. I understand I need to put them in an array such as countries[i] + " I will " + todo[i] but I'm uncertain how to do that. Would I need to do return instead of newArr =+ countries[i]? or am I completely wrong in my logic and in my code? all help in this would be appreciated.

3 Answers 3

1

If every key in your todo array corresponds to the key in the countries array, all you have to do is to loop your countries array and use the same key on your todo array.

var countries = [
    "Japan",
    "USA",
    "Sweden"
]

var todo = [
    "go to the park",
    "drink amazing whiskey"
]

for (var i = 0; i < countries.length; ++i) {
    // If key is not found in the `todo` array, skip entry in loop.
    if ( ! todo[i]) continue;
    
    console.log(countries[i] + ' ' + todo[i]);
}

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

2 Comments

There are no correlation. There are two arrays that I must iterate through and output them together as I mentioned. In the end it'll end up with country[i] + "whatever string" + todo[i].
@StevenOssorio Did you try the code? It produces the output you said you wanted. If that isn't what you meant, you should edit your question to clarify. You say there is no correlation, but your question suggests the input items are correlated by their position in the respective input lists.
0

If you need all todo items for each country then you would use nested loops like so:

var countries = [
 "Japan",
 "USA"
],
todo = [
 "go to the park",
 "drink amazing whiskey"
],
combined = [];

function myTrip(countries, todo){
  var newArr = [];

  countries.forEach(function(country) {
    todo.forEach(function(todo) {
        newArr.push(country + " I will " + todo);
    })
  });

  return newArr;
}

combined = myTrip(countries, todo);

alert(combined);

4 Comments

Possibly not. I'm actually answering a problem similar to what I wrote and they are giving us two arrays. Wish for us to output it together. I was thinking of nesting for-loops but that didn't seem conventional which is why I kept them separate.
You would use a nested loop if you wanted to output each todo item for each country.
So I'm suppose to nest them. Take the output of todo for example and place it together to whatever output that comes from country next and use the country loop to pass in the sentence to the newArr?
@StevenOssorio I edited my answer with nested loops to give you all todo ouputs for each country
0

You can do like this. Loop over one of the array & add the content from both the array. Keep these results in a separate array which you can retrieve

var countries = [
 "Japan",
 "USA"
]

var todo = [
 "go to the park",
 "drink amazing whiskey"
]

var outputObj=[]
countries.forEach(function(item,index){
   outputObj.push(countries[index] +' '+todo[index]);

})
console.log(outputObj)

DEMO

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.