I am not particularly experienced with javascript or coffeescript but I have managed to create a simple function using coffeescript and jQuery. The function is used to highlight and show/hide games for a tournament organising website so that it automatically hides and unchecks the games that shouldn't be selected and only shows one undecided game at a time. The function works perfectly with all but one caveat:
validate = ->
finished = true
$('.edit').find('.round').find('.match').each (m, match) ->
wins = {}
valid = true
$(match).find('.game').each (g, game) ->
if valid
$(game).show()
$(game).find('label').css('background-color', 'red')
checked = $(game).find(':checked')
if checked.length == 0
valid = false
else
winner = checked.attr('value')
if winner of wins
if ++wins[winner] == 2
valid = false
else
wins[winner] = 1
$('label[for=' + checked.attr('id') + ']').css('background-color', 'green')
else
$(game).find(':checked').prop('checked', false)
$(game).hide()
if finished
done = false
for p, w of wins
if w == 2
done = true
if not done
finished = false
if finished
$('input:submit').show()
else
$('input:submit').hide()
The function will refuse to work unless I write something, anything, after
if checked.length == 0
valid = false
on the same indentation as the valid = false
And I mean anything. I could set a new variable there and it works fine!
I have scoured around trying to find out if this is some indentation error and it just ignored the line. I've tried
valid = false if checked.length == 0
But I just get an error from rails because I have an else statement after that if. Please can someone explain to me what is going on.