HTML Academy
Swapping the elements
Arrays19/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
The minimum element is found!
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Looking for the minimum element

We’ve practiced element-swapping enough. Let’s summarize why an auxiliary variable is needed. Suppose there is an array in which we swap the first and second elements without the auxiliary variable:

var numbers = [1, 2, 3];

// Now numbers are [2, 2, 3]
numbers[0] = numbers[1];

If we immediately write the value of the second element in the first place, we will lose the value of the first element. Therefore, you first you need to store the value of the first element in a variable:

var numbers = [1, 2, 3];

// Now 1 is stored in swap
var swap = numbers[0];

// Now numbers are [2, 2, 3]
numbers[0] = numbers[1];

// Now numbers are [2, 1, 3]
numbers[1] = swap;

The next step on the way to sorting is to search for the minimum element. And we will look for this element not in the whole array, but in its specified part.

To do this, let’s set up variable currentIndex. It will control the initial value of the loop variable. Note that the loop variable this time will be called j (this is another typical name).

Comments

  • script.js
JavaScript
var usersByDay = [4, 2, 1, 3]; console.log(usersByDay);

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. At the end of the program, add a variable currentIndex with a zero value.
  2. Then add a loop that increases the variable j from the value currentIndex + 1 to usersByDay.length - 1 inclusively. Value of j must increase by one after each iteration.
  3. Inside the loop, log the value of the j-th element of the array in the console.

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.