// Generated by CoffeeScript 1.9.3
(function() {
  $(function() {
    var emptyInput, randomLetter, random_question, startUp, update_keyHandler, update_question, words;
    this.storage = new LocalStorage();
    $("#question, #answer").hide();
    words = $.getJSON("spellings.json");
    $.when(words).done((function(d) {
      return startUp(d);
    }));
    $.when(words).fail(function(data, status, error) {
      console.log(status + error);
    });
    startUp = function(data) {
      $("#question, #answer").show();
      this.current = "";
      this.fail = 0;
      this.reveal = "";
      this.unfinished = [];
      update_question(data);
      return this.data = data;
    };
    random_question = function(data) {
      var choice, i, j, ref, ref1, results, results1;
      choice = Math.floor(Math.random() * data['words'].length);
      this.current = data['words'][choice]['name'];
      (function() {
        results = [];
        for (var i = 1, ref = this.current.length; 1 <= ref ? i <= ref : i >= ref; 1 <= ref ? i++ : i--){ results.push(i); }
        return results;
      }).apply(this).forEach(function() {
        return this.reveal = this.reveal.concat("_");
      });
      this.unfinished = (function() {
        results1 = [];
        for (var j = 0, ref1 = this.current.length - 1; 0 <= ref1 ? j <= ref1 : j >= ref1; 0 <= ref1 ? j++ : j--){ results1.push(j); }
        return results1;
      }).apply(this);
      return data['words'][choice];
    };
    update_question = function(data) {
      var entry, i, len, size, word;
      entry = random_question(data);
      update_keyHandler();
      words = entry.name.split(" ");
      size = 0;
      for (i = 0, len = words.length; i < len; i++) {
        word = words[i];
        size += word.length;
      }
      $("dd#hint_stat").empty().append("This entry is <b>" + size + " letters</b> long. It is also <b>" + words.length + " word(s)</b> long.");
      $("dd#hint_define").empty().append(entry['hint']);
      return $("dd#hint_sentence").empty().append(entry['example']);
    };
    emptyInput = function() {
      return $("input#input_answer").val("");
    };
    randomLetter = function() {
      var decide, n;
      if (this.reveal === this.current) {
        return;
      }
      decide = Math.floor(Math.random() * this.unfinished.length);
      n = this.unfinished[decide];
      this.reveal = this.reveal.substring(0, n) + this.current[n] + this.reveal.substring(n + 1);
      return this.unfinished.splice(decide, 1);
    };
    return update_keyHandler = function() {
      $("#input_answer").off();
      return $("#input_answer").keydown((function(_this) {
        return function(e) {
          var attempt, last;
          if (e.which === 13) {
            attempt = $("#input_answer").val();
            if (attempt.toLowerCase() === _this.current.toLowerCase()) {
              $(".alert").addClass("alert-success");
              $(".alert").empty().append("You had just spelt the word <b>" + _this.current + "</b> right!");
              $("#revealed").empty();
              _this.reveal = "";
              update_question(_this.data);
              return emptyInput();
            } else {
              _this.fail += 1;
              $(".alert").addClass("alert-danger");
              if (_this.fail > 1) {
                last = " times.";
              } else {
                last = " time.";
              }
              randomLetter();
              $("#revealed").empty().append(_this.reveal);
              $(".alert").empty().append("You have failed " + _this.fail + last);
              return emptyInput();
            }
          }
        };
      })(this));
    };
  });

}).call(this);

@@ -1533,16 +1533,22 @@
 ntry, i,
+ last,
  len, si
@@ -1786,24 +1786,124 @@
 th;%0A      %7D%0A
+      if (words.length %3E 1) %7B%0A        last = %22words%22;%0A      %7D else %7B%0A        last = %22word%22;%0A      %7D%0A
       $(%22dd#
@@ -1980,20 +1980,12 @@
 long
-. It is also
+ and
  %3Cb%3E
@@ -2009,15 +2009,20 @@
 + %22 
-word(s)
+%22 + last + %22
 %3C/b%3E
@@ -3079,17 +3079,19 @@
 ust spel
-t
+led
  the wor

update output