Ad
  • Custom User Avatar

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

  • Custom User Avatar

    Darn it, i didn't need the else! nice code

  • Default User Avatar
  • Custom User Avatar

    I mean, the answer literally is in the desciption.

  • 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

  • Default User Avatar
  • Custom User Avatar

    Hi!

    I've added some non numeric characters in random tests.
    Thanks for the feedback!

  • Default User Avatar

    the random testing is broken.

    Testing for 16 9 4 20 18 12 8 1 7 14 3 13 10 2 17 5 19 6 15 21
    It should work for random tests too: 1 should equal 11

    UPD:
    I was converting string to numbers.
    So this is my issu
    Nevertheless the description can be more clear for this case.
    Or use in random test non numeric characters.

  • Default User Avatar

    Use print in your code.

  • Default User Avatar

    Try more and you can add print to your code to find input values for examples where you fail.
    Had the same problem and then found that I have missed some cases.
    First case don't filtering out non alphanumeric characters.
    Second case where char in text string appearing more times then in original.

  • Custom User Avatar

    Great explanation, thank you!

  • Default User Avatar

    Thanks. The key point for me was to understand than we use apply of bind. And "bind" has "apply" just like any other function.

  • Custom User Avatar

    The sneaky trick in this one is that we are applying the bind function, not the input fn as you probably did.

    Let's break it down:

    fn.bind( scope, arg1, arg2, ... )

    bind, when called on a function f, returns a new function g that calls f with the provided scope, and optionally the provided arguments. It can be implemented like this:

    Function.prototype.bind = function(scope) {
        var fn = this,
            // arguments here are the original arguments
            args = Array.prototype.slice(arguments, 1);
        return function() {
            // this arguments refers to any additional arguments
            return fn.apply(scope, args.concat(arguments));
        };
    };
    

    This allows us to call g at a later time, and guarantee that it will apply to scope, as well as always have any arguments we provided ahead of time.

    fn.apply(scope, argArray)

    apply, when called on a function f, executes the function f as if it was called on the given scope, and passed in the provided args.

    So, these are equivilant

    fn.apply(scope, [1, 2, 3]);
    scope.fn(1, 2, 3); // assuming `fn` exists on the scope object
    

    fn.bind.apply(fn, args)

    This calls apply on the bind function, because we have unknown values we want to bind to the original fn.

    If we knew the arguments, then these would be equivilant:

    fn.bind.apply(fn, [fn, 1, 2, 3]);
    //             ^    ^
    //             |    '-- this is the scope for bind!
    //             '-- this is the scope for apply
    fn.bind(fn, 1, 2, 3);
    //       ^
    //       '-- this is the scope for bind
    

    This is why passing in the extra fn in arguments is OK. In either case, what is returned is a new function g, that will call fn with the arguments 1, 2, 3. By using apply, we can pass in the original arguments, without worrying about them.


    And that's basically it. We're using the built-in JavaScript function behavior to dynamically create a new function which will call the original function with the provided arguments.

  • Default User Avatar

    It is very neat. But how does it work? In my solution I sliced initial arguments. But in this solution we just pass whole. What happens with arguments[0]?