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

  • 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.

  • 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.

  • 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...
    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
  • Pff! I think the only think he revised was the kata's rank (from 4 to 5). I misread the revisions: https://www.codewars.com/kata/neutralization-reaction/revisions

    It is my 2nd published kata (and the first to be ranked) so forgive my nervousness and ignorance.

  • Ok, I'm approving the translation. Please review the updated code because I think Voile, who approved the kata, made a lot of changes and I didn't have time to see what changed. :)
    Thank you for your contribution!

  • Your translation seems fine and I want to approve it, but I want to ask you something first.

    I have fine tuned the test cases for the JS version, namely all test cases have guaranteed shuffled acid, bases data sets and explicit tests for NH4OH, which is a unique base and could be missed during the shuffling and slicing of the data. However, due to codewars' technical issues my republishing efforts seem to be constantly timing out.

    Do you think that if I approve this version it will be possible for you to republish the updated tests later on or will it be as difficult for you as it is for me and we must wait a while until things smooth out on codewars' back-end?

  • To save an easily accessable bookmark to your currently working kata you can add a star (top-left star link under the kata's name) or add it to a custom collection.

    Either way the kata's you have ever worked on (attempted to provide a solution) and are incomplete are saved in your profile page under the Kata section in the Unfinished tab.

    Notice that if you want the partial solution to be saved along with the reference to the kata, you must hit the Attempt button at least one time. If you run only the local test cases the partial solution will not be saved. At least this is how it worked so far and this may be subject to change.

  • Since you edited your response and my comment became obsolete, I edited it too to stay relevant in its context.
    Do you think that it is worth investing some time into setting more semantic names for functions and their arguments?

  • ...and the prize for the most unimaginative solution function name of the year (i.e. "solve") goes to... this kata!
    Other than that, however, it was a nice challenge. Congrats! :D

    PS: Actually there are several other katas sharing the same generic solution naming practices, but I thought I would just drop my comment on this one, because someone has to do it I guess.

  • Consider renaming all references of circle with circumcircle (or circumscribed circle). This is the proper name for the unique circle that touches every vertex in the polygon, and in which we are interested. Other than that I enjoyed this kata! :)

  • Java translation kumited. Please approve.

  • Try a regex that targets that last ',' and replace it in the formatted string or use lastIndexOf(',') to the same effect.

  • Loading more items...