This repository was archived by the owner on Apr 18, 2025. It is now read-only.
generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 51
NW6 | Pedram Amani | Module-JS1 | Week4 #191
Open
pedram-am
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
pedram-am:JavaScript-week4
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ebec93e
JS1-week4 | implement folder: .(ord-num) .test.js
pedram-am 8cbb007
JS1-week4 | implement folder: count.test.js
pedram-am 2f5b936
JS1-week4 | implement folder: is-prime.test.js
pedram-am 382453e
JS1-week4 | implement folder: repeat.test.js
pedram-am fe85bb2
JS1-week4 | investigate folder: find.js
pedram-am 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
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,47 @@ | ||
// Given a positive integer num, | ||
// When the isPrime function is called with num as input, | ||
// Then it should return a boolean representing whether the num is prime | ||
|
||
function isPrime(num) { | ||
if (isNaN(num) || num <= 0 || Number.isInteger(num) !== true) { | ||
return "Invalid input, please enter a positive integer number"; | ||
} else if (num === 2) { | ||
return true; | ||
} else if (num % 2 === 0 || num === 1) { | ||
return false; | ||
} else { | ||
for (let i = 3; i <= Math.sqrt(num); i += 2) { | ||
if (num % i === 0) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
test("returns a boolean representing whether the num is prime", function () { | ||
expect(isPrime(1)).toBe(false); | ||
expect(isPrime(2)).toBe(true); | ||
expect(isPrime(3)).toBe(true); | ||
expect(isPrime(4)).toBe(false); | ||
expect(isPrime(5)).toBe(true); | ||
expect(isPrime(6)).toBe(false); | ||
expect(isPrime(7)).toBe(true); | ||
expect(isPrime(8)).toBe(false); | ||
expect(isPrime(11)).toBe(true); | ||
expect(isPrime(0)).toBe( | ||
"Invalid input, please enter a positive integer number" | ||
); | ||
expect(isPrime(2.2)).toBe( | ||
"Invalid input, please enter a positive integer number" | ||
); | ||
expect(isPrime(0.5)).toBe( | ||
"Invalid input, please enter a positive integer number" | ||
); | ||
expect(isPrime(-7)).toBe( | ||
"Invalid input, please enter a positive integer number" | ||
); | ||
expect(isPrime("abc")).toBe( | ||
"Invalid input, please enter a positive integer number" | ||
); | ||
}); |
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.
Just small refactoring is possible here:
!Number.isInteger(num)
instead ofNumber.isInteger(num) !== true