Skip to content
16 changes: 15 additions & 1 deletion task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
//Your code here
function countAboveThreshold(numbers, threshold) {
let count = 0;

for (let i = 0; i < numbers.length; i = i + 1) {
if (numbers[i] > threshold) {
count = count + 1;
}
}

return count;
}

console.log(countAboveThreshold([1, 5, 10, 3], 4)); // returns 2
console.log(countAboveThreshold([7, 8, 9], 10)); // returns 0
console.log(countAboveThreshold([], 5)); // returns 0
28 changes: 28 additions & 0 deletions task-2/calculateAverage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, test, expect } from "vitest";
import { calculateAverage } from "./calculateAverage";

describe("calculateAverage checks", () => {
test("normal numbers → should give 4 for [2, 4, 6]", () => {
expect(calculateAverage([2, 4, 6])).toBe(4);
});

test("one number → should give back the same number", () => {
expect(calculateAverage([5])).toBe(5);
expect(calculateAverage([0])).toBe(0);
});

test("empty array → should give null", () => {
expect(calculateAverage([])).toBe(null);
});

test("has a non array input → should give null", () => {
expect(calculateAverage(null)).toBe(null);
expect(calculateAverage("hello")).toBe(null);
expect(calculateAverage(100)).toBe(null);
});

test("list with non number value → should give null", () => {
expect(calculateAverage([1, 2, "hello"])).toBe(null);
expect(calculateAverage([1, true, 3])).toBe(null);
});
});
Loading