diff --git a/task-1/count-above-threshold.js b/task-1/count-above-threshold.js index 7cf0418..6693fd2 100644 --- a/task-1/count-above-threshold.js +++ b/task-1/count-above-threshold.js @@ -1 +1,16 @@ //Your code here +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)); diff --git a/task-2/calculateAverage.test.js b/task-2/calculateAverage.test.js new file mode 100644 index 0000000..8a6df4f --- /dev/null +++ b/task-2/calculateAverage.test.js @@ -0,0 +1,25 @@ +import { describe, test, expect } from "vitest"; +import { calculateAverage } from "./calculateAverage.js"; + +describe("calculateAverage", () => { + test("returns the average for a normal array of numbers", () => { + expect(calculateAverage([2, 4, 6])).toBe(4); + }); + + test("returns the number itself when the array has one element", () => { + expect(calculateAverage([5])).toBe(5); + }); + + test("returns null for an empty array", () => { + expect(calculateAverage([])).toBeNull(); + }); + + test("returns null when input is not an array", () => { + expect(calculateAverage("123")).toBeNull(); + expect(calculateAverage(null)).toBeNull(); + }); + + test("returns null when the array contains a non-number value", () => { + expect(calculateAverage([1, 2, "3"])).toBeNull(); + }); +}); diff --git a/task-3/count-vowels-debug.js b/task-3/count-vowels-debug.js index 1ce292b..5c52317 100644 --- a/task-3/count-vowels-debug.js +++ b/task-3/count-vowels-debug.js @@ -1,7 +1,7 @@ function countVowels(text) { let count = 0; - for (let i = 0; i <= text.length; i++) { + for (let i = 0; i < text.length; i++) { if ( text[i] === "a" || text[i] === "e" || @@ -15,3 +15,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 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}`); +});