HTML Academy
Continue sorting
Arrays23/30
Back to the list of tasks
  • 1. Little Muffin and Big Data
  • 2. Average value
  • 3. Finishing the traffic analyzer
  • 4. New data
  • 5. A lot of data? An array!
  • 6. Reading an array element by index
  • 7. Using a variable as an index
  • 8. Array length
  • 9. Let’s hit arrays with loops!
  • 10. Summation in the loop
  • 11. Finishing refactoring
  • 12. One small analytical investigation
  • 13. Writing to an array by index
  • 14. The murderer is a butler!
  • 15. Summary of “Arrays”. Part 1
  • 16. Fifth program: Beginner decryptor
  • 17. Vague suspicions
  • 18. Swapping the elements
  • 19. Looking for the minimum element
  • 20. The minimum element is found!
  • 21. Starting sorting
  • 22. Continue sorting
  • 23. Finish sorting
  • 24. Testing the sorting
  • 25. Median of an odd number of elements
  • 26. Median of an even number of elements
  • 27. Green light
  • 28. The murderer is the butler, again!
  • 29. Summary of “Arrays”. Part 2
  • 30. Sixth program: Long jump records
Testing the sorting
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Finish sorting

The program for sorting an array of four elements is ready. Yes, it is inflexible, and it will have to be rewritten for arrays with a different number of elements. But you will be fine, because you already know how to refactor.

In the current implementation, there is a lot of repeating code where only the value of variable currentIndex changes. How to make the structure of the program better?

You can take a piece of code with a loop that finds a minimum value and moves it to the specified location, and wrap it in another loop. And in this outer loop, increase the variable currentIndex.

Yes. Loops can be nested inside each other.

Note that currentIndex should change not from zero to array length, but from zero to array length minus two, inclusive. Let’s recall the previous task where we finished sorting of the array after reaching the second last element (because the last element automatically turned out to be the maximum one).

Let’s write the condition for continuing the outer loop by analogy with the condition for continuing the inner loop:

// Loop to the second last element
currentIndex <= usersByDay.length - 2

// Loop to the last element
j <= usersByDay.length - 1

After you wrap the code in the outer loop, the duplicate currentIndex declaration will reset the counter on every iteration. The console may show “Too many messages” and “Timeout error”. Remove that declaration in the next goal so the loop can finish.

Comments

  • script.js
JavaScript
var usersByDay = [4, 2, 1, 3]; console.log(usersByDay); // Start loop here // Sorting from the first element var currentIndex = 0; var minValue = usersByDay[currentIndex]; for (var j = currentIndex + 1; j <= usersByDay.length - 1; j++) { if (usersByDay[j] < minValue) { minValue = usersByDay[j]; var swap = usersByDay[currentIndex]; usersByDay[currentIndex] = minValue; usersByDay[j] = swap; console.log('Swapping ' + swap + ' and ' + minValue); console.log('Current array: ' + usersByDay); } } console.log('Minimum element ' + minValue + ' is at index ' + currentIndex); // Complete loop here // Sorting from the second element console.log(usersByDay); currentIndex = 1; minValue = usersByDay[currentIndex]; for (var j = currentIndex + 1; j <= usersByDay.length - 1; j++) { if (usersByDay[j] < minValue) { minValue = usersByDay[j]; var swap = usersByDay[currentIndex]; usersByDay[currentIndex] = minValue; usersByDay[j] = swap; console.log('Swapping ' + swap + ' and ' + minValue); console.log('Current array: ' + usersByDay); } } console.log('Minimum element ' + minValue + ' is at index ' + currentIndex); // Sorting from the third element console.log(usersByDay); currentIndex = 2; minValue = usersByDay[currentIndex]; for (var j = currentIndex + 1; j <= usersByDay.length - 1; j++) { if (usersByDay[j] < minValue) { minValue = usersByDay[j]; var swap = usersByDay[currentIndex]; usersByDay[currentIndex] = minValue; usersByDay[j] = swap; console.log('Swapping ' + swap + ' and ' + minValue); console.log('Current array: ' + usersByDay); } } console.log('Minimum element ' + minValue + ' is at index ' + currentIndex);

What didn’t you like in this task?

Thanks! We’ll fix everything at once!

Console

The code has changed, click “Run” or turn autorun on.

Result

Goalscompleted
  1. Remove the entire code for sorting from the second and third elements.
  2. Then wrap all the code after the second line in a loop that increases the variable currentIndex from zero to usersByDay.length - 2 inclusive. The value of currentIndex must increase by one after each iteration.
  3. Inside this loop, remove the duplicate declaration of the variable currentIndex.

Cookies ∙ Privacy ∙ License Agreement ∙ About ∙ Contacts ∙ © HTML Academy OÜ, 2019−2026

VISAMastercard

Log in

or

Forgot your password?

Sign up

Sign up

or
Log in

Restore access

Have you forgotten your password or lost access to your profile? Enter your email connected to your profile and we will send you a link to restore access.

Forgot to connect your email to the profile? Email us and we’ll help.