# JavaScript Challenge - Find Xth Number In Order

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!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was merged into an existing topic: Python Challenge - Find Xth Number In Order

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;
}
``````
1 Like

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(n2 )

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;
}
``````

Here is my example code. I needed to add the sort function check manually for it to work. !

`````` function getX(x, nums) {
``````

``````   const sorty = (a, b) => {
return a - b;

}

let arr = nums.sort(sorty);

arr.forEach((value, iterator) => {
if(iterator === x - 1){
}
})

}

console.log(getX(4, [5, 10, -3, -3, 7, 9]));

// Leave this so we can test your code:
module.exports = getX;``````

const getX = (x, nums) => x < 1 || x > nums.length ? 0 : nums.sort((a,b) => a-b)[x-1]

1 Like
``````function getX(x, nums) {

nums.sort((a, b) => a - b);

if(x < 1 || x > nums.length){
return 0;
}

return nums[x - 1];

}

module.exports = getX;
``````

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.