-
Notifications
You must be signed in to change notification settings - Fork 19
Baraah A. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Baraah A. #5
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,3 +18,5 @@ export function calculateAverage(numbers) { | |
|
|
||
| return sum / numbers.length; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import {describe , it , expect} from "vitest"; | ||
| import {calculateAverage} from "./calculateAverage"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| 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(); | ||
| }); | ||
|
|
||
| }); | ||
| 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 |
| 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"); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should call the function |
||
| 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"); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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.jsonis missing. This task should be also configured to install the packagevitestand have a script that runs the tests.More info: https://vitest.dev/guide/#adding-vitest-to-your-project