• JavaScript Teaser: A Missing Character

    Summary
    The function getCount() defined below is supposed to return the next number every time it's called, starting with 1. (So, the first call would return 1, the next call would return 2, the next would return 3, and so on.)

    Reading time: 1 minute
    Written by
    Date published
    May 29, 2015

    The function getCount() defined below is supposed to return the next number every time it's called, starting with 1. (So, the first call would return 1, the next call would return 2, the next would return 3, and so on.)

      function getCount() {
        var magic = function() {
          getCount = function() {
            return magic.x += !magic();
          };
        };
        return magic.x = 0 + magic();
      }
    

    Unfortunately, due to a one missing character, it currently returns NaN every time. Fix the code by adding the missing character.

    Please post the answer on JSFiddle or similar service, and then post a link to your answer below. You can also post other thoughts or email Rangle.io's CTO, Yuri@rangle.io, if you wish to share further ideas.

    Tags

    See what Ranglers are writing about on our blog