small medium large xlarge

Back to: All Forums  CoffeeScript
Generic-user-small
22 Sep 2011, 05:20
Brendan Johnston (1 post)

I found the random letter function a little different to how I would think about it.

I would write:

tileCounts =
  A: 9, B: 2, C: 2, D: 4, E: 12, F: 2, G: 3, H: 2, I: 9, J: 1, K: 1, L: 4
  M: 2, N: 6, O: 8, P: 2, Q: 1, R: 6, S: 4, T: 6, U: 4, V: 2, W: 2, X: 1
  Y: 2, Z: 1

tileLookup = []

for letter, count of tileCounts 
	tileLookup.push(letter) for [1..count] 

randomLetter = () ->
	randomNumber = Math.floor(Math.random() * tileLookup.length)
	tileLookup[randomNumber]

This would work better with flattened comprehensions. Because you could write:

tileLookup = letter for [1..count] for letter, count of tileCounts 
Photo_on_2010-05-01_at_22.39_pragsmall
28 Sep 2011, 07:55
John H Simmonds (13 posts)

Nice .. ? but did you mean to write

tileLookup.push letter for [1..count] for letter, count of tileCounts
You must be logged in to comment