Coding Challenge Practice – Question 30

The task is to reorder an array, given that we have an array of items and another array of indexes, so that A[i] is put at the index B[i].

The boilerplate code:

function sort(items, newOrder) {
// reorder items inline
}

Create a copy of the…


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to reorder an array, given that we have an array of items and another array of indexes, so that A[i] is put at the index B[i].

The boilerplate code:

function sort(items, newOrder) {
  // reorder items inline
}

Create a copy of the array, so that the data isn't overwritten too early.

const copy = items.slice();

For each index in the copied array, move the item in that index to the original array, based on the index of the second array.

for (let i = 0; i < newLength.array; i++) {
   items[newOrder[i]] = copy[i];
}

The final code for the sort function:

function sort(items, newOrder) {
  // reorder items inline
  const copy = items.slice();

  for(let i = 0; i < newOrder.length; i++) {
    items[newOrder[i]] = copy[i]
  }
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-10-18T22:52:36+00:00) Coding Challenge Practice – Question 30. Retrieved from https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/

MLA
" » Coding Challenge Practice – Question 30." Bukunmi Odugbesan | Sciencx - Saturday October 18, 2025, https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/
HARVARD
Bukunmi Odugbesan | Sciencx Saturday October 18, 2025 » Coding Challenge Practice – Question 30., viewed ,<https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 30. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/
CHICAGO
" » Coding Challenge Practice – Question 30." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/
IEEE
" » Coding Challenge Practice – Question 30." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 30 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/10/18/coding-challenge-practice-question-30/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.