Ad
  • Custom User Avatar

    I don't remember if the JS translatiion is from Jack or from Giacomo and I can't edit it anymore!

  • Custom User Avatar

    The false is not the problem.
    When you return false, you are giving the correct answer instead of the other wrong stuff.
    When you give a correct answer, you move on to the next batch of tests which is where your code fails.

    The major bug in the code is here

    for (i=0; i<array1.length; i++)
    

    Always use var i. As of now, you are modifying the global variable i.
    I actually can't believe that the test code is making the same mistake and using a global i instead of a var i.

    As a result, your function and the test code are messing with each other. I am not really sure how exactly the submission timeout error is occuring, but this mutual interaction is definitely the cause.

    Also, the sort function by default uses string sort, even if the arguments are numbers.
    So, [9, 100, 20, 25] will get sorted as [100, 20, 25, 9] instead of [9, 20, 25, 100].

    Sometimes this difference in sorting won't matter. Sometimes it will.

    How to sort an array of integers correctly

  • Custom User Avatar

    The problem in the code is not the type of numbers.
    The problem is that you are making a fixed size array at the start.

    Here is what happens when i = 2, j = 0 i.e. the third iteration of the main loop.
    arrayTable[i] is arrayTable[2] but arrayTable has just 2 elements, so arrayTable[i] is undefined.

    Now, if you were using a single dimensional array like arrayTable[i] = 5;, it wouldn't have mattered. Because the last value of arrayTable[i] is irrelevant in that case. Javascript will just add the element to the array.

    But the problem arises when you are using multi-dimensional arrays.
    Your code is trying to access arrayTable[i][j] which is basically undefined[j].
    This is not possible. Which is exactly what the error is telling you.

    You need to make sure your arrayTable has at least row number of rows. Or you can push a new array at the end of table before each iteration.
    In any case, you have to make sure a row exists before trying to use it.

  • Custom User Avatar

    Could you post your entire code (just mark it as spoiler)?
    I tried the kata and it's working for me.
    And it's really difficult to locate the problem in the code with only an if statement.