Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
I don't remember if the JS translatiion is from Jack or from Giacomo and I can't edit it anymore!
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
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 avar 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
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]
isarrayTable[2]
butarrayTable
has just 2 elements, soarrayTable[i]
isundefined
.Now, if you were using a single dimensional array like
arrayTable[i] = 5;
, it wouldn't have mattered. Because the last value ofarrayTable[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 basicallyundefined[j]
.This is not possible. Which is exactly what the error is telling you.
You need to make sure your
arrayTable
has at leastrow
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.
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.