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
14 changes: 14 additions & 0 deletions task-1/count-above-threshold.js
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
//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));// returns 2
console.log(countAboveThreshold([7,8,9],10));// returns 0
console.log(countAboveThreshold([],5));// returns 0
2 changes: 2 additions & 0 deletions task-2/calculateAverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ export function calculateAverage(numbers) {

return sum / numbers.length;
}


33 changes: 33 additions & 0 deletions task-2/calculateAverage.test.js
Copy link

Choose a reason for hiding this comment

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

I am unable to run the test suite because package.json is missing. This task should be also configured to install the package vitest and have a script that runs the tests.

More info: https://vitest.dev/guide/#adding-vitest-to-your-project

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {describe , it , expect} from "vitest";
import {calculateAverage} from "./calculateAverage";
Copy link

Choose a reason for hiding this comment

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

Node is unable to find resolve this import because there are two files that are named calculateAverage minus the file extension.


describe("calculateAverage",() =>{

it("returns the average for an array of valid numbers", ()=>{
const result = calculateAverage([2,4,6]);
expect(result).toBe(4);
});

it("returns the same number when array has one element", ()=>{
const result = calculateAverage([5]);
expect(result).toBe(5);
});

it("returns null when the array is empty", ()=>{
const result = calculateAverage([]);
expect(result).toBeNull();
});

it("returns null when input is not an array", ()=>{
expect(calculateAverage("123")).toBeNull();
expect(calculateAverage(null)).toBeNull();
expect(calculateAverage(123)).toBeNull();

});

it("returns null when the array contains a non-number value", ()=>{
const result = calculateAverage([1,2,"3"]);
expect(result).toBeNull();
});

});
17 changes: 11 additions & 6 deletions task-3/count-vowels-debug.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
function countVowels(text) {
let count = 0;

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

return count;
}

console.log(countVowels("hello"));// returns 2
console.log(countVowels("javascript"));// returns 3
console.log(countVowels(""));// returns 0
63 changes: 62 additions & 1 deletion task-4/search-experiment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
// 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 linearSearch(array , target){
for(let i=0; i < array.length; i++){
if(array[i] === target){
return i;
}
}
return -1;
}

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 target = 123456789;

const array1 =generateBigArray(1000);
const array2 =generateBigArray(100000);
const array3 =generateBigArray(10000000);

console.time("linear search 1k");
linearSearch(array1, target);
console.timeEnd("linear search 1k");

console.time("linear search 100k");
linearSearch(array2, target);
console.timeEnd("linear search 100k");

Copy link

Choose a reason for hiding this comment

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

Linear search for 1M is missing.

console.time("linear search 10m");
linearSearch(array3, target);
console.timeEnd("linear search 10m");

console.time("binary search 1k");
linearSearch(array1, target);
Copy link

Choose a reason for hiding this comment

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

This should call the function binarySearch. Also have a look at the code below this.

console.timeEnd("binary search 1k");

console.time("binary search 100k");
linearSearch(array2, target);
console.timeEnd("binary search 100k");

console.time("binary search 10m");
linearSearch(array3, target);
console.timeEnd("binary search 10m");