Advertisement
menixator

Array join - Dynamic seperators

Nov 24th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // joining arrays with dynamic separators.
  2.  
  3. // Array::reduce(fn(prev, curr, index, list), initprev)
  4.  
  5. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].reduce(function(prev, curr, key) {
  6.     // prev is the joined section. on the first loop it's ''(initprev).
  7.     return prev +
  8.         (key === 0 ?
  9.             '' :
  10.             ['#', '$', '!', '@', '*'][Math.floor(Math.random() * 5)]
  11.         ) +
  12.         curr
  13. }, '')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement