From 0128cc2a10ff0a7dfce3e4f11c04a926bd14fd42 Mon Sep 17 00:00:00 2001 From: atomiks Date: Tue, 19 Dec 2017 10:58:11 +1100 Subject: [PATCH] Make median.md immutable `.sort()` mutates the original array --- snippets/median.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/median.md b/snippets/median.md index fd8ca3d7c..c7eea609c 100644 --- a/snippets/median.md +++ b/snippets/median.md @@ -7,7 +7,7 @@ Return the number at the midpoint if `length` is odd, otherwise the average of t ```js const median = arr => { - const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b); + const mid = Math.floor(arr.length / 2), nums = [...arr].sort((a, b) => a - b); return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; }; // median([5,6,50,1,-5]) -> 5