Use Array.apply to repeat a loop

In JavaScript we cannot easily repeat something N times, and
CoffeeScript's range loop compiles into complex JavaScript.

We could also use `Array.from({ length: N })`, but that's not supported
by old browsers like Internet Explorer 11.
This commit is contained in:
Javi Martín
2019-07-01 00:43:52 +02:00
parent 00d7c92e86
commit ba325526ae
2 changed files with 10 additions and 15 deletions

View File

@@ -3,14 +3,13 @@
App.Managers =
generatePassword: ->
chars = "aAbcdeEfghiJkmnpqrstuUvwxyz23456789"
pass = ""
x = 0
while x < 12
i = Math.floor(Math.random() * chars.length)
pass += chars.charAt(i)
x++
return pass
possible_chars = "aAbcdeEfghiJkmnpqrstuUvwxyz23456789"
chars = Array.apply(null, length: 12).map ->
i = Math.floor(Math.random() * possible_chars.length)
possible_chars.charAt(i)
chars.join("")
togglePassword: (type) ->
$("#user_password").prop "type", type

View File

@@ -2,14 +2,10 @@
App.Polls =
generateToken: ->
token = ""
rand = ""
for n in [0..5]
rand = Math.random().toString(36).substr(2) # remove `0.`
token = token + rand
strings = Array.apply(null, length: 6).map ->
Math.random().toString(36).substr(2) # remove `0.`
token = token.substring(0, 64)
return token
strings.join("").substring(0, 64)
replaceToken: (token) ->
$(".js-question-answer").each ->