mirror of
https://gitlab.com/lecarore/breakout71.git
synced 2025-04-20 20:16:16 -04:00
wip
This commit is contained in:
parent
b0d8827e09
commit
4fb4c97734
15 changed files with 4190 additions and 3754 deletions
19
src/game_utils.ts
Normal file
19
src/game_utils.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
|
||||
|
||||
export function getMajorityValue(arr: string[]): string {
|
||||
const count: { [k: string]: number } = {};
|
||||
arr.forEach((v) => (count[v] = (count[v] || 0) + 1));
|
||||
// Object.values inline polyfill
|
||||
const max = Math.max(...Object.keys(count).map((k) => count[k]));
|
||||
return sample(Object.keys(count).filter((k) => count[k] == max));
|
||||
}
|
||||
|
||||
|
||||
export function sample<T>(arr: T[]): T {
|
||||
return arr[Math.floor(arr.length * Math.random())];
|
||||
}
|
||||
|
||||
export function sumOfKeys(obj:{[key:string]:number} | undefined | null){
|
||||
if(!obj) return 0
|
||||
return Object.values(obj)?.reduce((a,b)=>a+b,0) ||0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue