Got FATAL ERROR on a coding challenge

I need help to improve my solution. I got FATAL ERROR in a coding challenge in codewars site. That is mean my solution is not working with big inputs like:


My code so far:

function overTheRoad(address, n) {
  let odd = [];
  let even = [];
  for (let i = 2; i <= n * 2; i += 2) {
  for (let i = 1; i <= n * 2; i += 2) {
  even = even.reverse();

  return address % 2 === 0
    ? odd[even.indexOf(address)]
    : even[odd.indexOf(address)];



Training on Over The Road | Codewars

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

  • Why do you expect it does not work?

Just a thought.

What is the general formula for the address across the street, in logical terms?

(Hint: The answer should probably begin with “If the number is even…”)

I work hard and I find the solution:

function overTheRoad(address, n) {
  return (2 * n - address) + 1;
1 Like

To expand a little bit on where this came from.

There exists a general formula for the number components of any given row of a table with n rows constructed in such a way that the odd numbers ascend in the left column and the even numbers descend in the right.

Consider n=3.
n is our number of rows. So we’re going to arrange the addresses of 6 houses.

Odd Even
1 6
3 4
5 2

Let’s take a look at the rows. Specifically, the sum of the rows.
1+6 = 7
3+4 = 7
5+2 = 7
Interesting. All the rows add to the same value.

Let’s try being general; n rows.

Odd Even
1 2n
3 2n-2
5 2n-4
2n-5 6
2n-3 4
2n-1 2

What happens if we sum these rows?
Well, 1+2n is… 2n+1.
3+2n-2 is … 2n+1…
5+2n-4 is … 2n+1…
hmm… what about the end ones though…
2n-5+6 is 2n+1,
2n-3+4 is 2n+1,
2n-1+2 is 2n+1…
So every row adds to a value - 2n +1.

Let’s take that as a formula then.
For any two addresses in this system that are across the street from each other, a and b, a+b = 2n + 1.
Note that it doesnt matter which of the two is even or odd; a and b are interchangable.
(Also, intuitively, we can say that the sum of a row must be odd, because one of the addresses is even, and the other is odd; odd+even = odd. Any odd number can be defined as 2m+1, where m is an integer.)

Then it becomes a simple algebra equation: Given address (a) and the number of rows n, what is b?
Solve for b.
a + b = 2n + 1
b = 2n + 1 - a
(There’s no actual need for parenthesis, or the order of adding/subtracting: (2n+1)-a = (2n-a)+1.)

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.