Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function medianScore(scores) {
  2.     let result = [];
  3.     let current_arr = [];
  4.  
  5.     errorHandling(scores)
  6.  
  7.     for (let index = 0; index < scores.length; index++) {
  8.         for (let j = 0; j <= index; j++) {
  9.             current_arr.push(scores[index]);
  10.             current_arr.sort((a, b) => a - b);
  11.             getMiddleNumber(current_arr, result)
  12.             break;
  13.         }
  14.     }
  15.  
  16.     return result;
  17. }
  18.  
  19. console.log(medianScore([100, 20, 50, 70, 45]))
  20.  
  21. function getMiddleNumber(arr, result) {
  22.     if (arr.length % 2 == 1) {
  23.         let number = Math.ceil(arr[(arr.length - 1) / 2]);
  24.         result.push(number);
  25.     } else {
  26.         let number = Math.ceil((arr[(arr.length / 2)] + arr[(arr.length / 2 - 1)]) / 2);
  27.         result.push(number)
  28.     }
  29. }
  30.  
  31. function errorHandling(arr) {
  32.     if (arr.length < 1) {
  33.         throw new Error('Please insert any grade');
  34.     }
  35.     if (!(Array.isArray(arr))) {
  36.         throw new Error('Input is not in the correct format')
  37.     }
  38.     if (arr.some(isNaN)) {
  39.         throw new Error('The grades must be only numbers');
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement