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 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
Starting sorting
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

The minimum element is found!

Why is the initial value of the loop variable set to currentIndex + 1? The element at currentIndex is our first candidate for the minimum; the loop compares it with the following elements.

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 do we find the minimum value from the current position to the end of the array?

  1. Add variable minValue to store the minimum value.
  2. Suppose that the element at currentIndex is the minimum one. Therefore, let’s store the value of the element at currentIndex 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 currentIndex declaration, declare minValue with the value usersByDay[currentIndex].
  2. In the loop, remove the console log and add an if statement with the condition usersByDay[j] < minValue.
  3. If the condition is true, assign the value of usersByDay[j] to minValue.
  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−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.