forked from HackYourFuture/core-assignment-week-5
-
Notifications
You must be signed in to change notification settings - Fork 19
Hamed R. #14
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
Open
HamedRazizadeh-hub
wants to merge
2
commits into
HackYourAssignment:main
Choose a base branch
from
HamedRazizadeh-hub:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hamed R. #14
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; | ||
| } | ||
|
|
||
| const numbers = [1, 5, 8, 10, 3, 2, 6, 7, 4]; | ||
| const threshold = 4; | ||
| console.log(countAboveThreshold(numbers, threshold)); | ||
|
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. Very good test coverage. You included all required cases. However, I couldn’t see a ' package.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| */ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.