From fdd4d570b8588c7755e97a7ce60dcaaa9ee0d7b2 Mon Sep 17 00:00:00 2001 From: Hamed Razizadeh Date: Wed, 11 Feb 2026 22:11:37 +0100 Subject: [PATCH 1/2] assignment week 5 --- task-4/search-experiment.js | 80 ++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) diff --git a/task-4/search-experiment.js b/task-4/search-experiment.js index 63c189a..bee7e7d 100644 --- a/task-4/search-experiment.js +++ b/task-4/search-experiment.js @@ -1,3 +1,81 @@ // 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"; + +// Linear Search +function linearSearch(array, target) { + for (let i = 0; i < array.length; i++) { + if (array[i] === target) { + return i; + } + } + return -1; +} + +// Binary Search +function binarySearch(array, target) { + let left = 0; + let right = array.length - 1; + + while (left <= right) { + const middle = left + Math.floor((right - left) / 2); + + if (array[middle] < target) { + left = middle + 1; + } else if (array[middle] > target) { + right = middle - 1; + } else { + return middle; + } + } + + return -1; +} + +const array1k = generateBigArray(1_000); +const array100k = generateBigArray(100_000); +const array10M = generateBigArray(10_000_000); +const target = 123456789; + +// Linear Search timings +console.time("Linear Search 1k"); +linearSearch(array1k, target); +console.timeEnd("Linear Search 1k"); + +console.time("Linear Search 100k"); +linearSearch(array100k, target); +console.timeEnd("Linear Search 100k"); + +console.time("Linear Search 10M"); +linearSearch(array10M, target); +console.timeEnd("Linear Search 10M"); + +console.log("---"); + +// Binary Search timings +console.time("Binary Search 1k"); +binarySearch(array1k, target); +console.timeEnd("Binary Search 1k"); + +console.time("Binary Search 100k"); +binarySearch(array100k, target); +console.timeEnd("Binary Search 100k"); + +console.time("Binary Search 10M"); +binarySearch(array10M, target); +console.timeEnd("Binary Search 10M"); + +/* +test results: +Linear Search 1k: 0.368ms +Linear Search 100k: 0.972ms +Linear Search 10M: 44.328ms +--- +Binary Search 1k: 0.116ms +Binary Search 100k: 0.021ms +Binary Search 10M: 0.012ms + +showing that binary search is significantly faster than linear search, especially as the size of the array increases. +*/ From e14aff5512bdd4428bb854a11cb795054109e3a3 Mon Sep 17 00:00:00 2001 From: Hamed Razizadeh Date: Wed, 11 Feb 2026 22:12:42 +0100 Subject: [PATCH 2/2] assignment week 5 --- task-1/count-above-threshold.js | 17 +++++++++++++ task-2/calculateAverage.test.js | 45 +++++++++++++++++++++++++++++++++ task-3/count-vowels-debug.js | 27 +++++++++++++++++++- 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 task-2/calculateAverage.test.js diff --git a/task-1/count-above-threshold.js b/task-1/count-above-threshold.js index 7cf0418..df16c73 100644 --- a/task-1/count-above-threshold.js +++ b/task-1/count-above-threshold.js @@ -1 +1,18 @@ //Your code here +function countAboveThreshold(numbers, threshold) { + let count = 0; + let aboveThresholdNumbers = []; + + for (let num of numbers) { + if (num > threshold) { + count++; + aboveThresholdNumbers.push(num); + } + } + aboveThresholdNumbers.sort((a, b) => a - b); + return { count, numbers: aboveThresholdNumbers }; +} + +const numbers = [1, 5, 8, 10, 3, 2, 6, 7, 4]; +const threshold = 4; +console.log(countAboveThreshold(numbers, threshold)); diff --git a/task-2/calculateAverage.test.js b/task-2/calculateAverage.test.js new file mode 100644 index 0000000..7f54844 --- /dev/null +++ b/task-2/calculateAverage.test.js @@ -0,0 +1,45 @@ +import { describe, it, expect } from "vitest"; +import { calculateAverage } from "./calculateAverage"; + +// A normal case with valid numbers +describe("calculateAverage - valid numbers", () => { + it("should return the average of multiple numbers", () => { + expect(calculateAverage([2, 4, 6])).toBe(4); + }); +}); + +// An array with a single number +describe("calculateAverage - single number", () => { + it("should return the number itself if array has one element", () => { + expect(calculateAverage([5])).toBe(5); + }); +}); + +// An empty array +describe("calculateAverage - empty array", () => { + it("should return null for an empty array", () => { + expect(calculateAverage([])).toBeNull(); + }); +}); + +// A non-array input +describe("calculateAverage - non-array input", () => { + it("should return null for a string input", () => { + expect(calculateAverage("123")).toBeNull(); + }); + + it("should return null for null input", () => { + expect(calculateAverage(null)).toBeNull(); + }); +}); + +// An array containing a non-number value +describe("calculateAverage - array contains non-number", () => { + it("should return null if array contains a string", () => { + expect(calculateAverage([1, 2, "3"])).toBeNull(); + }); + + it("should return null if array contains undefined", () => { + expect(calculateAverage([1, 2, undefined])).toBeNull(); + }); +}); diff --git a/task-3/count-vowels-debug.js b/task-3/count-vowels-debug.js index 1ce292b..43eea61 100644 --- a/task-3/count-vowels-debug.js +++ b/task-3/count-vowels-debug.js @@ -1,7 +1,21 @@ function countVowels(text) { let count = 0; + const originalText = text; - for (let i = 0; i <= text.length; i++) { + text = text.toLowerCase(); + + console.log("Word:", originalText); + console.log("Character count:", text.length); + + let indexLine = ""; + for (let i = 0; i < text.length; i++) { + indexLine += `[${i}:${text[i]}] `; + } + console.log("Characters:", indexLine); + + console.log("\nChecking vowels..."); + + for (let i = 0; i < text.length; i++) { if ( text[i] === "a" || text[i] === "e" || @@ -10,8 +24,19 @@ function countVowels(text) { text[i] === "u" ) { count++; + console.log( + `Vowel found at index ${i}: "${text[i]}" → total vowels: ${count}`, + ); } } + console.log("\nTotal vowels:", count); + return count; } + +countVowels("hackyourfuture"); +countVowels("hello"); +countVowels("javascript"); +countVowels(""); +countVowels("HELLO");