• Recursion is beauty here!

  • The problem was with my code indeed! I simplified the solution and it passed normally on Codewars now.

    I have my local repo setup with TS, mocha and ts-node and I execute the test task in package.json to verify that all tests pass.

      ...
      "test": "mocha \"test/**/*.{ts,js}\" --recursive -r \"ts-node/register\" -r \"tsconfig-paths/register\"",
      ...
    

    It's with this setup that my erroneous code passed and I want to investigate further why that was the case.
    When I executed via a classic node cli terminal locally, it threw the same memory error as Codewars did!

    I will post here my findings when I find out what magic did the TS compiler and my code passed in the first iteration.

  • You might be running into an infinite loop or something... The error says you tried to allocate too much memory, possibly for a very big array. I'd bet that's from your code, and not the tests. Did you try logging stuff to console to see if that's the case?

  • In such case, it's difficult to tell why it does not work without seeing it. Kata seems to be OK.

  • It throws this error even with the sample tests which use simple and small arrays. The same sample tests and even more complicated ones with object references pass with flying colors locally. This memory error is very strange. That's why I posed the question tentatively and not marked as an issue.

  • Just solved the kata in JS and encountered no problems.

    Did you try to run similar test case, with similar sizes of arrays, locally in your IDE? There's a big chance that it will fail for you also locally (this FAQ could be helpful).

  • I've authored a JS solution that works on my machine (at least for various cases that I created and the sample tests provided), however when trying on CodeWars I get this stacktrace:

    <--- Last few GCs --->
    
    [1:0x563c8a521000]     3487 ms: Mark-sweep 580.5 (592.2) -> 580.5 (592.2) MB, 198.8 / 0.0 ms  allocation failure GC in old space requested
    [1:0x563c8a521000]     3686 ms: Mark-sweep 580.5 (592.2) -> 580.4 (585.2) MB, 199.4 / 0.0 ms  last resort GC in old space requested
    [1:0x563c8a521000]     3888 ms: Mark-sweep 580.4 (585.2) -> 580.4 (585.2) MB, 201.4 / 0.0 ms  last resort GC in old space requested
    
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 0x2ef7f64a57c1 <JSObject>
        1: combine [/home/codewarrior/index.js:~5] [pc=0x371d80b0c4e2](this=0x15be678c211 <JSGlobal Object>)
        2: arguments adaptor frame: 4->0
        3: /* anonymous */ [/home/codewarrior/index.js:36] [bytecode=0x3b47ab84d949 offset=222](this=0x15be678c211 <JSGlobal Object>)
        4: /* anonymous */ [/runner/frameworks/javascript/cw-2.js:152] [bytecode=0x3b47ab846851 offset=145](this=0x15be678c211 <JS...
     
     STDERR
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
    

    Can someone please tell me if this error originates from my code or it's a CodeWars platform error?
    The general error at the end reads "JavaScript heap out of memory" yet I am not using memory intensively, working with indices mostly, nor am I modifying input arrays.

  • Because the best practice in JavaScript is to create an array using the array literal notation ([]) and not calling explicitly the constructor (Array).

    The reason is that the Array constructor has a signature with mixed semantics (overloaded in a way, if JavaScript supported overloaded methods): when called with a single integer argument it produces an empty array of x empty places, whereas when called with more than one argument is creates an array with the supplied arguments as its elements. This can be a tricky situation which can lead to hard-to-catch errors.

    I usually only use the one argument version of the Array constructor to replace a classic for loop in a quick fashion. The recipe is simple: create an x length array in a simple call, fill it with some default value, and generate the sequence of desired values by mapping using the callback's index to produce the next value. For example,

    const a = Array(100).fill(0).map((_,i) => i+1); // ints [1, 100]
    

    Fan fact, the Array constructor can be called without the new keyword as well. The following are equivalent:

    const a = new Array(1, 2);
    const a = Array(1, 2);
    const a = [1, 2];
    

    But do not confuse this facility with other built-in primitive object wrappers which when called without new create the primitive value while with new yield an object wrapping the primitive value.

    's' === String('s)  // true
    's' === new String('s')  // false
    's' === new String('s').valueOf()  // true
    
    // same goes for Boolean and Number
    
  • really interesting approach - going to try it next time

  • I would very much like to see your diagram but the link is dead...

  • description conflicts, you'll have to fork the translation, using a fresh version of the description

  • very nice kata but it was quite simple to "problem-solve" (usually for 6kyu and harder -> they are devilish for problemsolving)
    It has been a long time (no pun intended) since I coded Java.

    I just used if-else statements and basic math, and maybe two loops to get the time from the input string xD ...
    Then I saw that other people used "snappy code" with all kinds of cool Java functions I was awestruck once again
    O_o

  • Great Kata! I approved it as a 6 kyu (average rank).

  • Loading more items...