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

Learn more about how to use this guide.

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!

Hi, so I tried this challenge, and it was supposed to be easy for me. I tried to keep it simple, but it only passes 8/10 tests.
I can’t figure out why.

To start, here is my solution right now:

function getX(x, nums) {
  let tempArray = nums.sort((a, b) => a - b);
  return tempArray[x - 1];
}

I sort the list, with comparison (to sort by number value and not by string value). Which is how I think you are supposed to do in this case.
Then I store it to a temporary variable from which I print the xTH smallest value, or so I thought…?
All my console logs I try with different arrays and so on, everything looks correct, so what the heck is wrong here? :stuck_out_tongue:

I assume that is because you don’t make a real copy of the original array. Instead, you are sorting the original array: Try logging the original array you pass as the 2nd argument after you ran the function. That will be ordered as well.

I’m having a similar issue as @manner89 . At first, I was doing exactly as he/she was, and also was passing only 8/10 tests. So I looked it up and found your reply over here, and tried to do as you suggested, but my code is still passing only 8/10 tests. Any other idea of what could possibly be wrong? Have you tried this challenge yourself?

My code after your suggestion:

function getX(x, nums) { // Write your code here nums.sort((a, b) => a - b); return nums[x -1]; }; console.log(getX(2, [5, 10, -3, -3, 7, 9]));

I’m still having the same issue that you had. Have you figured it out so far? This puzzle is itching my brain… :sweat_smile:

You also do not make a copy of the array ( that was my suggestion). Have you tried that, too?

Just tried an experiment. The instructions don’t say it, so there’s no way of knowing other than returning 0 is fairly common in challenges such as this when the requested index is out of bounds. If you include a check to make sure the index is in range, and return 0 if it isn’t, you should pass all 10 tests.

3 Likes