Advertisement
CoineTre

Robots Speed

Oct 9th, 2024 (edited)
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* The first batch of robots is ready. Now they need to be tested. All robots are unique, and each has its own speed. In this task, you need to find the robots' lowest, highest, and average speed.
  2.  
  3. Implement the getSpeedStatistic function, that accepts the testResults array of robots' speeds and returns statistics as an array with 3 numbers:
  4.  
  5. the first one is the lowest speed;
  6. the second is the highest speed;
  7. the last one is the average speed, rounded down (use Math.floor).
  8. Please note: if the testResults array is empty, return [0, 0, 0].
  9.  
  10. For example:
  11.  
  12. getSpeedStatistic([]); // [0, 0, 0]
  13. getSpeedStatistic([10]); // [10, 10, 10]
  14. getSpeedStatistic([8, 9, 3, 12]); // [3, 12, 8]
  15. getSpeedStatistic([10, 10, 11, 9, 12, 8]); // [8,
  16. */
  17.  
  18. function getSpeed(speeds){
  19. let min = speed[0];
  20. let max = speed[0];
  21. let aver = 0;
  22. for(let i of speeds){
  23.    
  24.    if(i < min){
  25.     min = i;
  26.    }
  27.    if(i > max){
  28.     max = i;
  29.    }
  30.    aver += i;
  31.    }
  32.  
  33. return [min,max];
  34. }
  35. console.log(getSpeed([8,9,3,12]));
  36.  
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement