This community-built FAQ covers the “Find Xth Number In Order” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.

Top Discussions on the JavaScript challenge Find Xth Number In Order

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

It would only accept my version when I did:
when there is no Xth number, return 0 (instead of undefined).

Minimal version using .sort()
although I had to provide a comparison function to it for it to work properly.

const compare = (a, b) => {
if (a < b) { return -1; }
else if (a > b) { return 1; }
else { return 0; }
}
const getX = (x, nums) => {
const result = Array.from(nums).sort(compare)[x - 1];
return result ? result : 0;
}

Here’s another version where I made an iterator (using a generator function) and did a simple sorting algorithm in it (not in-place).
The algorithm I used is: find min, insert it in new array, find min of remaining stuff, insert it, etc. 0(n^{2} )

my code (long)

I split the tasks into 3 functions, instead of writing more loops inside loops.

function min(nums, bools, changeMinToFalse) {
// finds minimum only for positions where bools has true
let minimum = undefined;
let indexOfMin = undefined;
const length = nums.length;
for (let i = 0; i < length; i++) {
if ((bools == undefined) || bools[i]) {
if ((minimum == undefined) || (nums[i] < minimum)) {
minimum = nums[i];
indexOfMin = i;
}
}
}
if (changeMinToFalse && !(indexOfMin == undefined)) {
bools[indexOfMin] = false;
}
return minimum;
}
function* inOrder(arr) {
// generator function to iterate from least to greatest
const length = arr.length;
const notUsedYet = new Array(length);
notUsedYet.fill(true);
for (let i = 0; i < length; i++) {
yield min(arr, notUsedYet, true);
}
}
function getX(x, nums) {
const length = nums.length;
if (x < 0) { x = length + x; }
const iterator = inOrder(nums);
for (let i = 1; i <= length; i++) {
if (i == x) {
return iterator.next().value;
}
else {
iterator.next();
}
}
return 0;
/* had to return 0 instead of undefined
because of answer checker */
}

Thank you! I returned “Error” instead of 0 for broken inputs and failed testcases and I couldn’t find out why. The instructions missing some points. As someone who learned C, returning 0 when the function is broken is very painful.

Your solution with BubbleSort is radically faster than other
Test: x=500, array with 1000 random integer from -50 to 50 (warming up, 1000 calls of 20 passes)

0.022 Array.sort

1.536 janbazant1107978602’s solution with generator