From 66c2dd3bf2d4eba529ab7f4eca725c94d6a4aea1 Mon Sep 17 00:00:00 2001 From: "dagimgit config --global user.email dagimvanhaileselassie@gmail.com" Date: Wed, 11 Feb 2026 10:58:09 +0100 Subject: [PATCH 1/4] implement threshold counting logic --- task-1/count-above-threshold.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/task-1/count-above-threshold.js b/task-1/count-above-threshold.js index 7cf0418..4eb2495 100644 --- a/task-1/count-above-threshold.js +++ b/task-1/count-above-threshold.js @@ -1 +1,15 @@ -//Your code here +export function countAboveThreshold(numbers, threshold) { + let count = 0; + + for (let i = 0; i < numbers.length; i++) { + if (numbers[i] > threshold) { + count++; + } + } + + return count; +} + +console.log(countAboveThreshold([1, 5, 10, 3], 4)); +console.log(countAboveThreshold([7, 8, 9], 10)); +console.log(countAboveThreshold([], 5)); From a63ed7abbbbfe5e7c451d12aa28919b5e0e9396f Mon Sep 17 00:00:00 2001 From: "dagimgit config --global user.email dagimvanhaileselassie@gmail.com" Date: Wed, 11 Feb 2026 11:02:48 +0100 Subject: [PATCH 2/4] test: complete task-2 unit testing --- task-2/countAboveThreshold.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 task-2/countAboveThreshold.test.js diff --git a/task-2/countAboveThreshold.test.js b/task-2/countAboveThreshold.test.js new file mode 100644 index 0000000..8a48782 --- /dev/null +++ b/task-2/countAboveThreshold.test.js @@ -0,0 +1,25 @@ +import { describe, it, expect } from "vitest"; +import { calculateAverage } from "./calculateAverage.js"; + +describe("calculateAverage", () => { + it("returns the average for a normal array of numbers", () => { + expect(calculateAverage([2, 4, 6])).toBe(4); + }); + + it("returns the number itself when the array has one element", () => { + expect(calculateAverage([5])).toBe(5); + }); + + it("returns null for an empty array", () => { + expect(calculateAverage([])).toBeNull(); + }); + + it("returns null when input is not an array", () => { + expect(calculateAverage("123")).toBeNull(); + expect(calculateAverage(null)).toBeNull(); + }); + + it("returns null when the array contains a non-number value", () => { + expect(calculateAverage([1, 2, "3"])).toBeNull(); + }); +}); From 82732e2aa67e4341f507c0370e22a01d6b6388c9 Mon Sep 17 00:00:00 2001 From: "dagimgit config --global user.email dagimvanhaileselassie@gmail.com" Date: Wed, 11 Feb 2026 11:06:03 +0100 Subject: [PATCH 3/4] fix: debug and correct logic for task-3 --- task-3/count-vowels-debug.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/task-3/count-vowels-debug.js b/task-3/count-vowels-debug.js index 1ce292b..d2fa5f5 100644 --- a/task-3/count-vowels-debug.js +++ b/task-3/count-vowels-debug.js @@ -1,7 +1,9 @@ function countVowels(text) { let count = 0; - for (let i = 0; i <= text.length; i++) { + for (let i = 0; i < text.length; i++) { + let char = text[i].toLowerCase(); // Convert to lowercase to catch "A" and "a" + if ( text[i] === "a" || text[i] === "e" || @@ -15,3 +17,7 @@ function countVowels(text) { return count; } +console.log(countVowels("hello")); // returns 2 +console.log(countVowels("javascript")); // returns 3 +console.log(countVowels("")); // returns 0 +console.log(countVowels("Apple")); // returns 1 From d9fc0a3e09805d4876dd135a4e11a9d58037d7a2 Mon Sep 17 00:00:00 2001 From: "dagimgit config --global user.email dagimvanhaileselassie@gmail.com" Date: Wed, 11 Feb 2026 11:08:41 +0100 Subject: [PATCH 4/4] feat: implement linear and binary search --- task-4/search-experiment.js | 54 ++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/task-4/search-experiment.js b/task-4/search-experiment.js index 63c189a..6f41107 100644 --- a/task-4/search-experiment.js +++ b/task-4/search-experiment.js @@ -1,3 +1,55 @@ // Write your code here // Use generateBigArray to create a big array numbers. -// Example: generateBigArray(1000000) will create an array of 1 million numbers. \ No newline at end of file +// Example: generateBigArray(1000000) will create an array of 1 million numbers. + +import generateBigArray from "./bigArray.js"; + +function linear_search(array, target) { + for (let i = 0; i < array.length; i++) { + if (array[i] === target) return target; + } + return "unsuccessful"; +} + +function binary_search(array, target) { + let left = 0; + let right = array.length - 1; + + while (left <= right) { + let middle = Math.floor(left + (right - left) / 2); + if (array[middle] < target) { + left = middle + 1; + } else if (array[middle] > target) { + right = middle - 1; + } else { + return middle; + } + } + return "unsuccessful"; +} + +const target = 123456789; +const experiments = [ + { size: 1000, label: "1k" }, + { size: 100000, label: "100k" }, + { size: 1000000, label: "1M" }, + { size: 10000000, label: "10M" }, +]; + +// --- Linear Search Tests --- +experiments.forEach((exp) => { + const arr = generateBigArray(exp.size); + console.time(`Linear Search ${exp.label}`); + linear_search(arr, target); + console.timeEnd(`Linear Search ${exp.label}`); +}); + +console.log("---"); + +// --- Binary Search Tests --- +experiments.forEach((exp) => { + const arr = generateBigArray(exp.size); + console.time(`Binary Search ${exp.label}`); + binary_search(arr, target); + console.timeEnd(`Binary Search ${exp.label}`); +});