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
17 changes: 17 additions & 0 deletions task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -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 };

Choose a reason for hiding this comment

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

Good job with the logic, you correctly identified and counted the numbers above the threshold!

However, the task requires returning only the count. Right now, the function returns an object with extra data. It's always good to stick to requirements.

}

const numbers = [1, 5, 8, 10, 3, 2, 6, 7, 4];
const threshold = 4;
console.log(countAboveThreshold(numbers, threshold));
45 changes: 45 additions & 0 deletions task-2/calculateAverage.test.js

Choose a reason for hiding this comment

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

Very good test coverage. You included all required cases. However, I couldn’t see a ' package.jsonorVitest`. Remember, it’s always good to try to actually run the tests.

Original file line number Diff line number Diff line change
@@ -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();
});
});
27 changes: 26 additions & 1 deletion task-3/count-vowels-debug.js
Original file line number Diff line number Diff line change
@@ -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" ||
Expand All @@ -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");
80 changes: 79 additions & 1 deletion task-4/search-experiment.js
Original file line number Diff line number Diff line change
@@ -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.
// 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.
*/