Files
HowBadIsMyBatch/docs/Utils.js
frankknoll d3acdd66f4 refactoring
2023-10-15 13:55:50 +02:00

28 lines
841 B
JavaScript

class Utils {
// adapted from https://www.w3resource.com/javascript-exercises/fundamental/javascript-fundamental-exercise-88.php
static median(arr) {
const mid = Math.floor(arr.length / 2);
const nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
}
static sum(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
static getKeysAlignedWithValues(dict) {
const keys = [];
const values = [];
for (const [key, value] of Object.entries(dict)) {
keys.push(key);
values.push(value);
}
return { 'keys': keys, 'values': values };
}
static sliceDict(dict, start, end) {
return Object.fromEntries(Object.entries(dict).slice(start, end));
}
}