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
15 changes: 15 additions & 0 deletions task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -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));
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, 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();
});
});
6 changes: 5 additions & 1 deletion task-3/count-vowels-debug.js
Original file line number Diff line number Diff line change
@@ -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" ||
Expand All @@ -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

Choose a reason for hiding this comment

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

should return 2 but as it is the 'A' is not considered a vowel. If you want to include capital and small caps letters you could convert the vowel to lowercase first:
text[i].toLowerCase() === "a"
... etc

54 changes: 53 additions & 1 deletion task-4/search-experiment.js
Original file line number Diff line number Diff line change
@@ -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.
// 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}`);
});