HTML Academy
The minimum element is found!
Arrays21/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 from an array using index
  • 7. Variable as 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
Continue sorting
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Starting sorting

We learned how to find the minimum value. Why not take another step.

Now let’s not just calculate the minimum value after the first element, but write this value into the place of the first element. To do this, you need to add a little to the algorithm:

  1. Add variable minValue to store the minimum value.
  2. Suppose that the first element is the minimum one. Therefore, let’s store the value of the first element in minValue before the loop.
  3. At each iteration of the loop, we compare the current element with minValue value.
  4. If the current element is less than minValue, write it into minValue and then swap the value of the first element and of the current one.

After the loop is completed, the element with the minimum value will appear in the first position of the array. All other elements will be larger.

Comments

  • script.js
JavaScript
var usersByDay = [4, 2, 1, 3]; console.log(usersByDay); var currentIndex = 0; var minValue = usersByDay[currentIndex]; for (var j = currentIndex + 1; j <= usersByDay.length - 1; j++) { if (usersByDay[j] < minValue) { minValue = usersByDay[j]; } } console.log('Minimum element: ' + minValue);

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. Inside the condition after the variable minValue, save the value usersByDay[currentIndex] to variable swap. Write the remaining code inside the condition.
  2. On the next line in usersByDay[currentIndex], write down the minimum value.
  3. Then write value of variable swap in usersByDay[j].
  4. On the next line, log in the console: 'Swapping ' + swap + ' and ' + minValue.
  5. Below, log in the console: 'Current array: ' + usersByDay.

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

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.