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
10 changes: 9 additions & 1 deletion task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
//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, 2, 3, 20, 100], 9));
58 changes: 58 additions & 0 deletions task-2/calculateAverage-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { test, expect } from "vitest";
import { calculateAverage } from "./calculateAverage.js";

test("returns the average of numbers in an array", () => {
const numbers = [2, 4, 6];

const result = calculateAverage(numbers);

expect(result).toBe(4);
});

test("returns the single element when array has one number", () => {
const numbers = [5];

const result = calculateAverage(numbers);

expect(result).toBe(5);
});

test("returns null for empty array", () => {
const numbers = [];

const result = calculateAverage(numbers);

expect(result).toBe(null);
});

test("returns null for non-array input", () => {
const numbers = "123";

const result = calculateAverage(numbers);

expect(result).toBe(null);
});

test("returns null when the array contains a non-number value", () => {
const numbers = [1, 2, "3"];

const result = calculateAverage(numbers);

expect(result).toBe(null);
});

test("returns the average for array with negative numbers", () => {
const numbers = [-2, -4, -6];

const result = calculateAverage(numbers);

expect(result).toBe(-4);
});

test("returns the average for array with floating point numbers", () => {
const numbers = [3.2, 1.2, 5.3];

const result = calculateAverage(numbers);

expect(result).toBe(3.233333333333333);
});
40 changes: 20 additions & 20 deletions task-2/calculateAverage.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
export function calculateAverage(numbers) {
if (!Array.isArray(numbers)) {
return null;
}
if (numbers.length === 0) {
return null;
}
let sum = 0;
for (const num of numbers) {
if (typeof num !== "number") {
return null;
}
sum += num;
}
return sum / numbers.length;
}
export function calculateAverage(numbers) {
if (!Array.isArray(numbers)) {
return null;
}

if (numbers.length === 0) {
return null;
}

let sum = 0;

for (const num of numbers) {
if (typeof num !== "number") {
return null;
}
sum += num;
}

return sum / numbers.length;
}
15 changes: 7 additions & 8 deletions task-3/count-vowels-debug.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
function countVowels(text) {
let count = 0;

for (let i = 0; i <= text.length; i++) {
const lowerCase = text.toLowerCase();
for (let i = 0; i < lowerCase.length; i++) {
if (
text[i] === "a" ||
text[i] === "e" ||
text[i] === "i" ||
text[i] === "o" ||
text[i] === "u"
lowerCase[i] === "a" ||
lowerCase[i] === "e" ||
lowerCase[i] === "i" ||
lowerCase[i] === "o" ||
lowerCase[i] === "u"
) {
count++;
}
}

return count;
}
7 changes: 2 additions & 5 deletions task-4/bigArray.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
// you can use this function to generate a big array of the specified size
// It contains numbers from 0 to size - 1
// Example: generateBigArray(5) will return [0, 1, 2, 3, 4]
function generateBigArray(size) {
const array = new Array(size);
for (let i = 0; i < size; i++) {
array[i] = i;
}
return array;
};
}

export default generateBigArray;
export default generateBigArray;
63 changes: 60 additions & 3 deletions task-4/search-experiment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
// Write your code here
// Use generateBigArray to create a big array numbers.
// Example: generateBigArray(1000000) will create an array of 1 million numbers.
import generateBigArray from "./bigArray.js";

function getTargetByBinarySearch(array, target) {
let left = 0,
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 false;
}

function getTargetByLinearSearch(array, target) {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return false;
}

console.time("Linear Search 1k");
getTargetByLinearSearch(generateBigArray(1000), 10540640);

Choose a reason for hiding this comment

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

Array creation is included in the timing, this is wrong. Right now you are doing this:

getTargetByLinearSearch(generateBigArray(1000), 10540640);

This means you are measuring: Array creation time + Search time. You should create the array first, then measure only the search:

const arr1k = generateBigArray(1000);

console.time("Linear Search 1k");
getTargetByLinearSearch(arr1k, 10540640);
console.timeEnd("Linear Search 1k");

console.timeEnd("Linear Search 1k");

console.time("Linear Search 100k");
getTargetByLinearSearch(generateBigArray(100000), 10540640);
console.timeEnd("Linear Search 100k");

console.time("Linear Search 1M");
getTargetByLinearSearch(generateBigArray(10000000), 10540640);
console.timeEnd("Linear Search 1M");
Comment on lines +36 to +38

Choose a reason for hiding this comment

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

Label says 1M, but the array size used is 10000000 (10M). The label and value do not match.


console.time("Linear Search 10M");
getTargetByLinearSearch(generateBigArray(10000000), 10540640);
console.timeEnd("Linear Search 10M");

console.log("---");

console.time("Binary Search 1k");
getTargetByBinarySearch(getTargetByBinarySearch(1000), 10540640);

Choose a reason for hiding this comment

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

getTargetByBinarySearch(getTargetByBinarySearch(1000), 10540640) is wrong.
First argument must be an array, but here it calls binary search with 1000 instead of generateBigArray(1000). It shuld be:

getTargetByBinarySearch(generateBigArray(1000), 10540640);

console.timeEnd("Binary Search 1k");

console.time("Binary Search 100k");
getTargetByBinarySearch(generateBigArray(100000), 10540640);
console.timeEnd("Binary Search 100k");

console.time("Binary Search 1M");
getTargetByBinarySearch(generateBigArray(10000000), 10540640);
console.timeEnd("Binary Search 1M");
Comment on lines +54 to +56

Choose a reason for hiding this comment

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

Same here: Label says 1M, but the array size used is 10000000 (10M). The label and value do not match.


console.time("Binary Search 10M");
getTargetByBinarySearch(generateBigArray(10000000), 10540640);
console.timeEnd("Binary Search 10M");