Ad
  • Custom User Avatar
  • Custom User Avatar

    But CodeWars submissions are not executed in your browser.

    Also, the behavior is the same in every browser and even Node.js. You may only find problems when executing tests inside their consoles, as they might (Chrome, for instance, does) use call themselves.

  • Default User Avatar

    True. I had not dived into the inner working of console.log - however, have experienced a very similar problem without console.log.
    As you point out, mrkish, the end result is the same (recursion).
    My point is that working with Function.prototype.call/apply may give these problems - and I assume this could depend on the particular platform/browser you are running.

  • Custom User Avatar

    Actually, that's not how Javascript works. Function.prototype.call is not called for every function call.

    It just happens that CodeWars's console.log (or print) invokes call on Array.prototype.slice to do its thing. Of course, that means the end result is the same: calling console.log in call's body will end up in an infinite recursion. Calling other functions, in the other hand, is fine.

  • Default User Avatar

    Execution a function require a "call" to that function - as in f() where "f" is called using "()".
    Though this may not be exactly as is works, you can imagine all JS function calls underneath are translated to the "generic" .call - which does the magic to set up "this" and "arguments" among other things.
    So - calling a function like "f()" can be thought of as running "f.call(this)" (and app
    Using this "thought technique" having console.log() in the code expands to:

    Function.prototype.call = function() {
    console.log.call(this,"my logging text");
    }

    Essentially calling call to expand the call....
    Hope this clarify my line :)

  • Custom User Avatar

    Your function might be returning undefined. Are you returning anything?

  • Default User Avatar

    I also tried this, until I realised that javascript's sort algorithm wasn't itself stable. So unless you want to implement your own sort algorithm in the answer, it's not really sensible to try it this way.

  • Custom User Avatar

    Excellent point, updated the description.