Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function medianScore(scores) {
- let result = [];
- let current_arr = [];
- errorHandling(scores)
- for (let index = 0; index < scores.length; index++) {
- for (let j = 0; j <= index; j++) {
- current_arr.push(scores[index]);
- current_arr.sort((a, b) => a - b);
- getMiddleNumber(current_arr, result)
- break;
- }
- }
- return result;
- }
- console.log(medianScore([100, 20, 50, 70, 45]))
- function getMiddleNumber(arr, result) {
- if (arr.length % 2 == 1) {
- let number = Math.ceil(arr[(arr.length - 1) / 2]);
- result.push(number);
- } else {
- let number = Math.ceil((arr[(arr.length / 2)] + arr[(arr.length / 2 - 1)]) / 2);
- result.push(number)
- }
- }
- function errorHandling(arr) {
- if (arr.length < 1) {
- throw new Error('Please insert any grade');
- }
- if (!(Array.isArray(arr))) {
- throw new Error('Input is not in the correct format')
- }
- if (arr.some(isNaN)) {
- throw new Error('The grades must be only numbers');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement