From 581ca02918e55e8f8be721994e814bce75608d63 Mon Sep 17 00:00:00 2001 From: Rohit Tanwar <31792358+kriadmin@users.noreply.github.com> Date: Mon, 1 Jan 2018 16:09:29 +0530 Subject: [PATCH] Update join.md as suggested by @atomiks --- snippets/join.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/snippets/join.md b/snippets/join.md index f951070e5..93f1d0e02 100644 --- a/snippets/join.md +++ b/snippets/join.md @@ -3,11 +3,8 @@ Is like `Array.join()` but with an additional argument of `end`(is equal to `separator` by default) which is used to separate the second to last and last items in the array. Returns `""` when the array is empty and the first item when the length of array is 1. ```js -const join = (arr,separator = ',',end = separator ) => { - return arr.reduce((acc,val,i) => { - return i == arr.length - 2 ? acc + val + end : i == arr.length - 1 ? acc + val : acc + val + separator - },'') - } +const join = (arr, separator = ',', end = separator) => + arr.slice(0, arr.length - 1).join(separator) + end + arr[arr.length - 1] ``` ```js