Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7927559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:22:13+00:00 2026-06-03T19:22:13+00:00

I’m an inexperienced JavaScript user working through the book CoffeeScript: Accelerated JavaScript Development ,

  • 0

I’m an inexperienced JavaScript user working through the book CoffeeScript: Accelerated JavaScript Development, in which the author makes a kind of Scrabble app (with Scrabble words in a 5 x 5 grid) to demonstrate some of the concepts in practice. I’m having a bit of trouble with the randomLetter method below. Can someone explain the code snippet below, with reference to these points:

i) the author does x = 1 but then seems to assign a letter to x with, x += tileCounts[letter]. Why do that?

ii) If, contrary to what I assume in (i), he is assigning a number rather than a letter to x, how does it work that ‘letter’ actually represents the number associated with each letter?

iii) based on what you see here, can you figure out why he would only return x if it was greater than the randomNumber? I realize there may not be enough information here to answer this question but if there is, please explain

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

totalTiles = 0
totalTiles += count for letter, count of tileCounts
alphabet = (letter for letter of tileCounts).sort()

randomLetter = ->  
  randomNumber = Math.ceil Math.random() * totalTiles
  x = 1
  for letter in alphabet
    x += tileCounts[letter]
    return letter if x > randomNumber
#END:moduleVars
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:22:15+00:00Added an answer on June 3, 2026 at 7:22 pm

    I agree that randomLetter() is difficult to understand. As the author of the book, let me try to clarify.

    The only form of randomness available to us in pure JavaScript is Math.random(), which returns a number between 0 and 1. So the idea of randomLetter() is to convert that random number into a random letter, with each letter having a probability of being drawn that’s proportionate to the number of Scrabble tiles with that letter.

    To make the analogy of what we’re simulating more precise, picture a stack of all the Scrabble tiles sorted from "A" to "Z". Then all we have to do is:

    1. Convert the output of Math.random() (with range 0 to 1) into something with range 0 to totalTiles (the size of the tile stack) and assign it to randomNumber.
    2. Go through the stack until we get to the randomNumber-th tile and return that letter.

    In hindsight, I should probably have written this function to more literally follow that analogy by first creating an array of all the tiles and then drawing random values from it. Instead, I opted for an indirect approach: Iterating through the letters of the alphabet, drawing all the tiles with that letter, and asking “Have we drawn randomNumber tiles yet? If so, we’ve reached our random letter.”

    So to give a short answer: x represents the number of tiles that have been drawn from the stack, starting by drawing all of the "A"s, then the "B"s, and so on.

    I hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.