HTML Academy
Looking for the minimum element
Arrays20/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
Starting sorting
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

The minimum element is found!

Why the initial value of the loop variable is set to currentIndex + 1? This is necessary to look for the minimum value after the element with position currentIndex.

var currentIndex = 0;
// Look for the minimum element starting from second position
// [4, 2, 1, 3]

var currentIndex = 1;
// Look for the minimum element starting from third position
// [4, 2, 1, 3]

How to find the minimum element located after the first one?

  1. Add variable minValue to store the minimum value.
  2. Suppose that the first element is the minumum 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, then write it in minValue.

If the first element was the minimum one, then value minValue in the loop will not change. If after the first element there were elements with a lesser value, this value will be written in minValue in the loop.

In any case, the minimum element will be found.

Please note, that this algorithm will not work for empty arrays. In order for the algorithm to work, we need at least one element to denote it as the minimum element before the loop begins.

Comments

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

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. After the variable currentIndex, add a variable minValue with the value usersByDay[currentIndex].
  2. Then in the loop, delete console log and add condition usersByDay[j] < minValue.
  3. When the condition is met, assign variable minValue value of the j-th element of the array.
  4. And in the same place log the following message in the console: 'New minimum element: ' + minValue.
  5. After the loop, log this message in the console: 'Minimum element: ' + minValue.

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.