Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
27 changes: 26 additions & 1 deletion task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
//Your code here
function countAboveThreshold(numbers, threshold) {
// Return the count of numbers greater than the threshold
// If the array is empty, return `0`
//Assume the input is valid
let count = 0;

for (const num of numbers) {
if (num > threshold) {
count++;
}
}

return count;
}

/*
example usage:
countAboveThreshold([1,5,10,3],4)// returns 2
countAboveThreshold([7,8,9],10)// returns 0
countAboveThreshold([],5)// returns 0
*/

console.log(countAboveThreshold([1,5,10,3],4));
console.log(countAboveThreshold([7,8,9],10));
console.log(countAboveThreshold([],5));

2 changes: 1 addition & 1 deletion task-2/calculateAverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ export function calculateAverage(numbers) {
}

return sum / numbers.length;
}
}
25 changes: 25 additions & 0 deletions task-2/calculateAverage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, it, expect } from "vitest";
import { calculateAverage } from "./calculateAverage.js";

describe("calculateAverage", () => {
it("normal case - valid numbers", () => {
expect(calculateAverage([2, 4, 6])).toBe(4);
});

it("single number array", () => {
expect(calculateAverage([5])).toBe(5);
});

it("empty array → null", () => {
expect(calculateAverage([])).toBeNull();
});

it("non-array input → null", () => {
expect(calculateAverage("123")).toBeNull();
expect(calculateAverage(null)).toBeNull();
});

it("array with non-number → null", () => {
expect(calculateAverage([1, 2, "3"])).toBeNull();
});
});
Loading