forked from HackYourFuture/core-assignment-week-5
-
Notifications
You must be signed in to change notification settings - Fork 19
Dagim H. #20
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
Unlock7
wants to merge
4
commits into
HackYourAssignment:main
Choose a base branch
from
Unlock7: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
Dagim H. #20
Changes from all commits
Commits
Show all changes
4 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,16 @@ | ||
| //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)); | ||
| console.log(countAboveThreshold([7, 8, 9], 10)); | ||
| console.log(countAboveThreshold([], 5)); |
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,25 @@ | ||
| import { describe, test, expect } from "vitest"; | ||
| import { calculateAverage } from "./calculateAverage.js"; | ||
|
|
||
| describe("calculateAverage", () => { | ||
| test("returns the average for a normal array of numbers", () => { | ||
| expect(calculateAverage([2, 4, 6])).toBe(4); | ||
| }); | ||
|
|
||
| test("returns the number itself when the array has one element", () => { | ||
| expect(calculateAverage([5])).toBe(5); | ||
| }); | ||
|
|
||
| test("returns null for an empty array", () => { | ||
| expect(calculateAverage([])).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when input is not an array", () => { | ||
| expect(calculateAverage("123")).toBeNull(); | ||
| expect(calculateAverage(null)).toBeNull(); | ||
| }); | ||
|
|
||
| test("returns null when the array contains a non-number value", () => { | ||
| expect(calculateAverage([1, 2, "3"])).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,55 @@ | ||
| // 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 linear_search(array, target) { | ||
| for (let i = 0; i < array.length; i++) { | ||
| if (array[i] === target) return target; | ||
| } | ||
| return "unsuccessful"; | ||
| } | ||
|
|
||
| function binary_search(array, target) { | ||
| let left = 0; | ||
| let right = array.length - 1; | ||
|
|
||
| while (left <= right) { | ||
| let middle = Math.floor(left + (right - left) / 2); | ||
| if (array[middle] < target) { | ||
| left = middle + 1; | ||
| } else if (array[middle] > target) { | ||
| right = middle - 1; | ||
| } else { | ||
| return middle; | ||
| } | ||
| } | ||
| return "unsuccessful"; | ||
| } | ||
|
|
||
| const target = 123456789; | ||
| const experiments = [ | ||
| { size: 1000, label: "1k" }, | ||
| { size: 100000, label: "100k" }, | ||
| { size: 1000000, label: "1M" }, | ||
| { size: 10000000, label: "10M" }, | ||
| ]; | ||
|
|
||
| // --- Linear Search Tests --- | ||
| experiments.forEach((exp) => { | ||
| const arr = generateBigArray(exp.size); | ||
| console.time(`Linear Search ${exp.label}`); | ||
| linear_search(arr, target); | ||
| console.timeEnd(`Linear Search ${exp.label}`); | ||
| }); | ||
|
|
||
| console.log("---"); | ||
|
|
||
| // --- Binary Search Tests --- | ||
| experiments.forEach((exp) => { | ||
| const arr = generateBigArray(exp.size); | ||
| console.time(`Binary Search ${exp.label}`); | ||
| binary_search(arr, target); | ||
| console.timeEnd(`Binary Search ${exp.label}`); | ||
| }); |
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.
should return 2 but as it is the 'A' is not considered a vowel. If you want to include capital and small caps letters you could convert the vowel to lowercase first:
text[i].toLowerCase() === "a"
... etc