-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Added FindMax to Maths #831
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
Changes from all commits
e17e0d0
79a2b16
7ae8d92
58bd7f4
4e34c7d
fa2186b
af752e9
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /** | ||
| * Function to find the maximum number given an array of integers | ||
| * Returns the maximum number of the array | ||
| * If the array is empty it returns the string 'Array is empty' | ||
| */ | ||
|
|
||
| export const findMax = (arr) => { | ||
| if (arr.length === 0) { return 'Array is empty' } | ||
|
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. Throw an error, not a string. 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. Agree with @raklaptudirm. Whenever you know the conditions where your function will fail (I call them nice errors) then throw then instead of handling them yourself (by returning a string) throw an error instead so that the user can handled them according to their business rules. |
||
|
|
||
| let max = arr[0] | ||
| arr.forEach(element => { | ||
| if (element > max) { | ||
| max = element | ||
| } | ||
| }) | ||
| return max | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import { findMax } from '../FindMax' | ||
|
|
||
| test('Should return the highest number in the array', () => { | ||
| const max = findMax([2, 5, 1, 12, 43, 1, 9]) | ||
| expect(max).toBe(43) | ||
| }) | ||
|
|
||
| test('Should return the highest number in the array', () => { | ||
| const max = findMax([21, 513, 6]) | ||
| expect(max).toBe(513) | ||
| }) | ||
|
|
||
| test('Should return the highest number in the array', () => { | ||
| const max = findMax([]) | ||
| expect(max).toBe('Array is empty') | ||
| }) |