From: Carl Worth Date: Wed, 10 Jun 2020 01:14:29 +0000 (-0700) Subject: Use spread syntax rather than fill(nill) in generate_id X-Git-Url: https://git.cworth.org/git?p=lmno-server;a=commitdiff_plain;h=1010e7bfe722481b7036934b6b859b8bb7916d52 Use spread syntax rather than fill(nill) in generate_id We don't actually need the array filled with null values specifically, we just need something that map() will act on (since it doesn't act on the empty entries we get from Array(4)). Using [...Array(4)] gives us an array of undefined values which does the trick, and is a bit more compact than .fill(null). Of course, I blow away any savings in compactness with the giant comment I've added here! --- diff --git a/lmno.js b/lmno.js index de95aa4..71ccc58 100644 --- a/lmno.js +++ b/lmno.js @@ -101,7 +101,11 @@ class LMNO { } generate_id() { - return Array(4).fill(null).map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join(''); + /* Note: The copy from Array(4) to [...Array(4)] is necessary so + * that map() will actually work, (which it doesn't on an array + * from Array(N) which is in this strange state of having "empty" + * items rather than "undefined" as we get after [...Array(4)] */ + return [...Array(4)].map(() => LMNO.letters.charAt(Math.floor(Math.random() * LMNO.letters.length))).join(''); } create_game(engine_name) {