Skip to content
Open
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
18 changes: 17 additions & 1 deletion task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
//Your code here
const countNumbersInArray = (numbers, threshold) => {
let count = 0;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use Array methods the same code can be written like this:

count = numbers.filter(num > num > threshold).length.

I'm not sure if Array prototype methods are already known at this stage of the course but it is definitely preferred over the traditional loop construct

count++;
}
}

return count;
};


console.log(`There is ${countNumbersInArray([90,45,76,3,5,8,2,77,3,65,43,4,45,90], 40)} numbers above the threshold.`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just nitpicking here - "numbers above the threshold" => "numbers above 40"

console.log(`There is ${countNumbersInArray([1,2,3,4,5,6,7,8,9], 5)} numbers above the threshold.`);
console.log(`There is ${countNumbersInArray([4,5,6,6,6,3,80,32], 90)} numbers above the threshold.`);
console.log(`There is ${countNumbersInArray([100,200,300,400,500], 250)} numbers above the threshold.`);
66 changes: 66 additions & 0 deletions task-2/caclculateAvetage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { test, expect } from "vitest";
import { calculateAverage } from "./calculateAverage.js";

test("returns the correct average for an array of valid numbers", () => {
expect(calculateAverage([2, 4, 6])).toBe(4);
});

test("returns the correct average for a single number", () => {
expect(calculateAverage([5])).toBe(5);
});

test("handles negative numbers correctly", () => {
expect(calculateAverage([-2, -4, -6])).toBe(-4);
});

test("handles decimal numbers correctly", () => {
expect(calculateAverage([1.5, 2.5, 3])).toBe(2.3333333333333335);
});

test("handles mixed positive and negative numbers", () => {
expect(calculateAverage([-10, 0, 10])).toBe(0);
});

test("returns null for an empty array", () => {
expect(calculateAverage([])).toBeNull();
});

test("returns null for a string input", () => {
expect(calculateAverage("123")).toBeNull();
});

test("returns null for null input", () => {
expect(calculateAverage(null)).toBeNull();
});

test("returns null for undefined input", () => {
expect(calculateAverage(undefined)).toBeNull();
});

test("returns null for a number input", () => {
expect(calculateAverage(123)).toBeNull();
});

test("returns null for an object input", () => {
expect(calculateAverage({ length: 3 })).toBeNull();
});

test("returns null if array contains a string", () => {
expect(calculateAverage([1, 2, "3"])).toBeNull();
});

test("returns null if array contains null", () => {
expect(calculateAverage([1, 2, null])).toBeNull();
});

test("returns null if array contains undefined", () => {
expect(calculateAverage([1, 2, undefined])).toBeNull();
});

test("returns null if array contains a boolean", () => {
expect(calculateAverage([1, 2, true])).toBeNull();
});

test("returns null if array contains an object", () => {
expect(calculateAverage([1, 2, {}])).toBeNull();
});
Loading