HTML Academy
Vague suspicions
Arrays18/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
Looking for the minimum element
  • Sign up
  • Log in

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Swapping the elements

Our fears came back to haunt us: there were even more severe dips in traffic than before. Apparently, marketers have reopened their cybersport club and are holding competitions on random days of the week. But how do they manage to cover the tracks?

They disguised the poor traffic. The analyzer always says that average traffic is good, and no one even thinks about looking for dips there. Clever!

How did they manage to hack the program? Look at the surge in traffic at the end of the month. This is marketers pumping low-quality traffic. This peak, in turn, affects the average traffic, and it creates an appearance of being good.

Traffic peak

It is necessary to improve the program and, in addition to the average attendance, consider median traffic, which is less sensitive to such outbursts. Median is a median value of an array. To get it, you first need to sort the array. Now let’s learn how to sort it.

During sorting, elements of an array are swapped. Usually this is done using an intermediate variable, into which one of the elements being swapped is stored. Try to rearrange the two elements by yourself.

Comments

  • script.js
JavaScript
var usersByDay = [4, 1, 2, 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

Let’s swap the first and second elements of the array.

  1. At the end of the program, add a variable swap with value usersByDay[0].
  2. Then, write the value of the element with index 1 into the element of the array with index 0.
  3. Log the array in the console below. Look, the first value is lost if we do not save it in a variable in advance.
  4. Then write the value of variable swap into the element of the array with index 1.
  5. And once again log 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.