Ad
  • Custom User Avatar

    Thinking about it, I'm pretty sure you are right. It would only unfluence the result before the summation (0 included or not).

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    It would be nice to specify what do with empty keys or values. That is, should the string

    foo=&bar=baz
    

    return {"foo": "", "bar": "baz"} or just {"bar": "baz"}? I implemented the former behavior, but I have seen other solutions that do the latter. I think it would be good to have a test case that demands the one or the other.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    While this solution works for free-standing functions as tested in this Kata, it would fall apart when applied to object methods. Consider this test case:

    function Rectangle(width, height) {
      this.width = width;
      this.height = height;
    }
    
    Rectangle.prototype.area = function() {
      return this.width * this.height;
    }
    
    Rectangle.prototype.area = Rectangle.prototype.area.wrap(function(original) {
      return original();
    });
    
    var rect = new Rectangle(2, 4);
    Test.assertEquals(rect.area(), 8);
    

    This would fail with expected NaN to equal 8 as the solution binds this to the value it had at the time of the call to wrap, rather than to rect at the time of the call of the wrapper.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I have almost the same answer, but with an explicit return 1 if exponent == 0. The || 1 trick is pretty clever, I would never have thought of that!

  • Custom User Avatar

    has_key? is generally considered deprecated and key? is preferred. See this comment of Ruby's creator Matz.