# Unexpected Algorithm Practice with Topcoder Front-End

I wanted to try Topcoder web arena. Unfortunately, the very first practice problem I tried never loaded. I logged in/out and tried several browsers, but no luck.

I decided to debug. I can barely understand JavaScript, but I hoped it would at least give me an idea of what’s happening.

Chrome debugger paused inside this code inside bundle.js:

`updateCoderPlacement = function (coders) {        coders.forEach(function (item) {            item.roomPlace = 1;            coders.forEach(function (other) {                if (item.userName !== other.userName &&                        item.totalPoints < other.totalPoints) {                    item.roomPlace += 1;                }                });            });        };`

It looked quadratic in the size of coders, and coders had 67374 items. That would explain the problem, since it was trying to go through 4 billion iterations before loading the data.

But what could it be doing?

After re-reading the code a few times, I had to accept that it’s just trying to calculate the rank of each item in `coders` based on the value of `.totalPoints`.

I was really surprised that topcoder of all websites would have code like this. Not to mention, wouldn’t they notice that the website doesn’t load?

In any case, it’s obvious that we need a rank_array function, but how to do it in JavaScript? Surprisingly SO search yielded O(N²) solution as the top result (an answer with a more efficient solution is right after). Is it really common to write code like this on the front-end?

Anyway, here’s my attempt at translating into JavaScript the solution I would have done in python:

`updateCoderPlacement = function (coders) {    sorted_idx = coders      .map(function(a, i) {        return i;               })      .sort(function(a, b) {        return coders[b].totalPoints - coders[a].totalPoints;               });          sorted_idx      .map(function(idx, i) {        coders[idx].roomPlace = i+1              });    };`

I guess topcoder worked out fine in the end. Even though I didn’t get to practice their algorithm problems in their web arena (which never loaded), the website itself provided a fun way to practice JavaScript.

Edit: I just realized that the code is actually open source. If someone wants to, please go ahead and submit a PR to fix this. I’m not familiar with JavaScript or front end. But it does seem that the bug is in resolvers.js which then gets compiled (?) into bundle.js.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.