0

Why isn't my code hitting the line that contains the alert?

window.Game = class Game
  constructor: ->
    rows: 22
    columns: 10
    board: []

createBoard: ->                                                                                                                                                                                           
  # Some code here...
  for x in [0...@columns]
    alert("THIS IS HERE")
  # More code down here...

1 Answer 1

2

Probably because @columns is undefined.

Your constructor:

constructor: ->
  rows: 22
  columns: 10
  board: []

simply creates an object and throws it away, it is the same as this:

constructor: ->
  o = {
    rows: 22
    columns: 10
    board: []
  }
  return

So no instance variables are set and your constructor doesn't do much at all. Perhaps you meant to say:

constructor: ->
  @rows = 22
  @columns = 10
  @board = []

or possibly:

constructor: (@rows = 22, @columns = 10, @board = [ ]) ->

I'm assuming that your createBoard method is actually indented one level so that it is a method in your Game class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.