简体   繁体   中英

How do I return a new array of all the inputs as strings?

instructions

Write a function called combiningThings. This function should:

create and return a new array of all inputs as strings each of the new strings should start with "[index] is [data]"

For example:

combiningThings([1, 2, 3]) // returns ["0 is 1", "1 is 2", "2 is 3"]
combiningThings(['My', 1, 'number']) // returns ["0 is My", "1 is 1", "2 is number"]

this is what I have so far but I think I'm making the array into a string rather than making a new array of strings:

var indexToString = function(arrayTwo) {
  var combine = "";
  for (var i = 0; i < arrayTwo.length; i++) {
    combine += arrayTwo.indexOf(i++) + " is " + arrayTwo[i++];
  }
  return combine;
};

this works with map:

 function combiningThings(arr) { return arr.map((v, index) => index + ' is ' + v); } console.log(combiningThings([1, 2, 3])); console.log(combiningThings(['My', 1, 'number'])); 

This should work:

function combiningThings(array) {
  return array.map((el, index) => {
    return index + ' is ' + el;
  });
}

combiningThings(['My', 1, 'number']); 

// This will give --> [ '0 is My', '1 is 1', '2 is number' ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM