Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats calculator jan 15 2 #64

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
57ef45a
[GREEN]: should know that for the numbers 1, 2 and 3, the minimum is 1
josegarrera Jan 14, 2025
29d71ea
[REFACTOR]: improved test
josegarrera Jan 15, 2025
6539aae
[GREEN]: should know that for the numbers 1, 2 and 3, the maximum is 3
josegarrera Jan 15, 2025
74c6b69
[REFACTOR]: add return type
josegarrera Jan 15, 2025
02203d9
[GREEN]: should know that for the numbers 1, 2 and 3, the number of e…
josegarrera Jan 15, 2025
9949c1b
[GREEN]: should know that for the numbers 1, 2 and 3, the average is 2
josegarrera Jan 15, 2025
e83a378
[GREEN]: should know that for the numbers 2, 3, 1 and 6 the minimum i…
josegarrera Jan 15, 2025
f92be73
[REFACTOR]: extract method
josegarrera Jan 15, 2025
b95ec6c
[GREEN]: should know that for the numbers 2, 4, 21, -8, 53 and 40 the…
josegarrera Jan 15, 2025
fab0e56
[REFACTOR]: improve naming and extract method
josegarrera Jan 15, 2025
a60522c
[GREEN]: should know that for the numbers 0, 0 and 0 the minimum is 0…
josegarrera Jan 15, 2025
a79d6e7
[GREEN]: should throw an error if the list contains a non number
josegarrera Jan 15, 2025
e8aac31
[GREEN]: should throw an error message "Input contains invalid number…
josegarrera Jan 15, 2025
5181b6e
[GREEN]: should throw an error message "Input must be a non-empty arr…
josegarrera Jan 15, 2025
8a41ef0
[GREEN]: should throw an error message "Input must be a non-empty arr…
josegarrera Jan 15, 2025
e2c19e7
[REFACTOR]: extract method
josegarrera Jan 15, 2025
a862dd6
[REFACTOR]: join first tests into one
josegarrera Jan 15, 2025
25878cb
[REFACTOR]: it.each for happy cases
josegarrera Jan 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,58 @@
import {Statistics, StatsCalculator} from "./index";

type HappyCase = [number[], Statistics];

describe('stats calculator', () => {
const happyCases: HappyCase[] = [
[[1, 2, 3], {
min: 1,
max: 3,
numberOfElements: 3,
average: 2
}],
[[2, 3, 1, 6], {
min: 1,
max: 6,
numberOfElements: 4,
average: 3
}],
[[2, 4, 21, -8, 53, 40], {
min: -8,
max: 53,
numberOfElements: 6,
average: 18.666666666666668
}],
[[0, 0, 0], {
min: 0,
max: 0,
numberOfElements: 3,
average: 0
}]
];
it.each(happyCases)("should know that for the numbers %s the result should be %s", (numbers, expected) => {
expect(StatsCalculator.run(numbers)).toEqual(expected)
})

it('should throw an error message \"Input contains invalid numbers (e.g., NaN or non-numeric values).\" if the list contains a non number', () => {
const numbers = [0, 0, "d"];

// @ts-ignore
expect(() => StatsCalculator.run(numbers)).toThrowError("Input contains invalid numbers (e.g., NaN or non-numeric values).");
})

it('should throw an error message \"Input must be a non-empty array of numbers.\" if the list is empty', () => {
const numbers: number[] = [];

expect(() => StatsCalculator.run(numbers)).toThrowError("Input must be a non-empty array of numbers.");
})

it('should throw an error message \"Input must be a non-empty array of numbers.\" if the input is an object', () => {
const numbers= {
a: 1,
b: 2
};

// @ts-ignore
expect(() => StatsCalculator.run(numbers)).toThrowError("Input must be a non-empty array of numbers.");
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export type Statistics = {
min: number,
max: number,
numberOfElements: number,
average: number
}

export class StatsCalculator {
static run(numbers: number[]): Statistics {

this.validate(numbers);

const min = this.minOf(numbers);
const max = this.maxOf(numbers);
const numberOfElements = this.numberOfElementsOf(numbers);
const average = this.averageOf(numbers);

return {
min,
max,
numberOfElements,
average
}
}

private static validate(numbers: number[]) {
if (this.isEmpty(numbers) || this.isNotArray(numbers)) {
throw new Error("Input must be a non-empty array of numbers.")
}

if (this.someIsNotANumber(numbers)) {
throw new Error("Input contains invalid numbers (e.g., NaN or non-numeric values).")
}
}

private static isEmpty(numbers: number[]) {
return numbers.length === 0;
}

private static isNotArray(numbers: number[]) {
return !Array.isArray(numbers);
}

private static someIsNotANumber(numbers: number[]) {
return numbers.some(num => typeof num !== "number" || isNaN(num));
}

private static minOf(numbers: number[]) {
return numbers.reduce((min, current) => current < min ? current : min);
}

private static maxOf(numbers: number[]) {
return numbers.reduce((max, current) => current > max ? current : max);
}

private static numberOfElementsOf(numbers: number[]) {
return numbers.length;
}

private static averageOf(numbers: number[]) {
return this.sumOf(numbers) / numbers.length;
}

private static sumOf(numbers: number[]) {
return numbers.reduce((total, current) => total + current, 0);
}
}