forked from HackYourFuture/core-assignment-week-5
-
Notifications
You must be signed in to change notification settings - Fork 19
Shadi A. #3
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
shmoonwalker
wants to merge
6
commits into
HackYourAssignment:main
Choose a base branch
from
shmoonwalker: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
Shadi A. #3
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a2c3aa
Implement countNumbersInArray function to count numbers above a speci…
shmoonwalker daeef85
write a unit test for all input,manage the edge cases and invalid input.
shmoonwalker 508c91a
debugged and test with difrent input
shmoonwalker 9b29475
Merge remote-tracking branch 'origin/main'
shmoonwalker 1aab6b4
implement linear and binary search algorithms
shmoonwalker 3fabce5
refactor search experiment to handle multiple array sizes and target …
shmoonwalker 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,17 @@ | ||
| //Your code here | ||
| const countNumbersInArray = (numbers, threshold) => { | ||
| let count = 0; | ||
|
|
||
| for (let i = 0; i < numbers.length; i++) { | ||
| if (numbers[i] > threshold) { | ||
| count++; | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
| }; | ||
|
|
||
|
|
||
| console.log(`There is ${countNumbersInArray([90,45,76,3,5,8,2,77,3,65,43,4,45,90], 40)} numbers above the 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. just nitpicking here - "numbers above the threshold" => "numbers above 40" |
||
| console.log(`There is ${countNumbersInArray([1,2,3,4,5,6,7,8,9], 5)} numbers above the threshold.`); | ||
| console.log(`There is ${countNumbersInArray([4,5,6,6,6,3,80,32], 90)} numbers above the threshold.`); | ||
| console.log(`There is ${countNumbersInArray([100,200,300,400,500], 250)} numbers above the threshold.`); | ||
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,66 @@ | ||
| import { test, expect } from "vitest"; | ||
| import { calculateAverage } from "./calculateAverage.js"; | ||
|
|
||
| test("returns the correct average for an array of valid numbers", () => { | ||
| expect(calculateAverage([2, 4, 6])).toBe(4); | ||
| }); | ||
|
|
||
| test("returns the correct average for a single number", () => { | ||
| expect(calculateAverage([5])).toBe(5); | ||
| }); | ||
|
|
||
| test("handles negative numbers correctly", () => { | ||
| expect(calculateAverage([-2, -4, -6])).toBe(-4); | ||
| }); | ||
|
|
||
| test("handles decimal numbers correctly", () => { | ||
| expect(calculateAverage([1.5, 2.5, 3])).toBe(2.3333333333333335); | ||
| }); | ||
|
|
||
| test("handles mixed positive and negative numbers", () => { | ||
| expect(calculateAverage([-10, 0, 10])).toBe(0); | ||
| }); | ||
|
|
||
| test("returns null for an empty array", () => { | ||
| expect(calculateAverage([])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for a string input", () => { | ||
| expect(calculateAverage("123")).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for null input", () => { | ||
| expect(calculateAverage(null)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for undefined input", () => { | ||
| expect(calculateAverage(undefined)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for a number input", () => { | ||
| expect(calculateAverage(123)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null for an object input", () => { | ||
| expect(calculateAverage({ length: 3 })).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null if array contains a string", () => { | ||
| expect(calculateAverage([1, 2, "3"])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null if array contains null", () => { | ||
| expect(calculateAverage([1, 2, null])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null if array contains undefined", () => { | ||
| expect(calculateAverage([1, 2, undefined])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null if array contains a boolean", () => { | ||
| expect(calculateAverage([1, 2, true])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null if array contains an object", () => { | ||
| expect(calculateAverage([1, 2, {}])).toBeNull(); | ||
| }); |
Oops, something went wrong.
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.
If you use Array methods the same code can be written like this:
count = numbers.filter(num > num > threshold).length.
I'm not sure if Array prototype methods are already known at this stage of the course but it is definitely preferred over the traditional loop construct