Goal
I have a working function (JSFiddle). On numerous occasions throughout a script the function runs sequentially. In these instances, there is a lot of repetitious code that I would like to consolidate.
Ideally changing code like this:
functionName("First_item") +
functionName("Second_item") +
functionName("Third_item") +
To something like this:
functionName("First_item", "Second_item", "Third_item");
The function will run for each item in the list so the result is the same but the code more elegant and maintainable.
Notes:
- I’m not looking to use any libraries (e.g. jQuery) to accomplish the goal.
Solution
Amit Joki’s answer kindly noted I could use arguments. When I implemented the code, the modified function (JSFiddle) only returned the
output
string for the first argument / item.Vanice’s answer pointed out the eventual solution.
- Make one string from the output of all arguments / items by concatenating (joining) the
output
strings within the for loop (with the use of+=
). - Return the concatenated output by placing the
return
outside of the for loop.
- Make one string from the output of all arguments / items by concatenating (joining) the
Example
Thanks
Thank you very much to everyone for their time and help on this. I really appreciate it!