-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Improved abs
function
#923
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
Merged
Merged
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,22 +1,22 @@ | ||
/** | ||
* @function absVal | ||
* @function abs | ||
* @description This script will find the absolute value of a number. | ||
* @param {Integer} num - The input integer | ||
* @return {Integer} - Absolute number of num. | ||
* @see [Absolute_value](https://en.wikipedia.org/wiki/Absolute_value) | ||
* @example absVal(-10) = 10 | ||
* @example absVal(50) = 50 | ||
* @example absVal(0) = 0 | ||
* @param {number} num - The input integer | ||
* @return {number} - Absolute number of num. | ||
* @see https://en.wikipedia.org/wiki/Absolute_value | ||
* @example abs(-10) = 10 | ||
* @example abs(50) = 50 | ||
* @example abs(0) = 0 | ||
*/ | ||
|
||
const absVal = (num) => { | ||
// Find absolute value of `num`. | ||
'use strict' | ||
if (num < 0) { | ||
return -num | ||
const abs = (num) => { | ||
const validNumber = +num // converted to number, also can use - Number(num) | ||
|
||
if (Number.isNaN(validNumber)) { | ||
throw new TypeError('Argument is NaN - Not a Number') | ||
} | ||
// Executes if condition is not met. | ||
return num | ||
|
||
return validNumber < 0 ? -validNumber : validNumber // if number is less then zero mean negative then it converted to positive. i.e -> n = -2 = -(-2) = 2 | ||
} | ||
|
||
export { absVal } | ||
export { abs } |
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,18 +1,39 @@ | ||
import { absVal } from '../Abs' | ||
import { abs } from '../Abs' | ||
|
||
describe('Testing abs function', () => { | ||
it('Testing for invalid types', () => { | ||
expect(() => abs('234a')).toThrow() | ||
expect(() => abs({})).toThrow() | ||
expect(() => abs([12, -32, -60])).toThrow() | ||
}) | ||
|
||
it('Testing for number of string type', () => { | ||
expect(abs('-345')).toBe(345) | ||
expect(abs('-345.455645')).toBe(345.455645) | ||
}) | ||
|
||
it('Testing for a boolean type', () => { | ||
expect(abs(true)).toBe(1) | ||
expect(abs(false)).toBe(0) | ||
}) | ||
|
||
describe('absVal', () => { | ||
it('should return an absolute value of a negative number', () => { | ||
const absOfNegativeNumber = absVal(-34) | ||
const absOfNegativeNumber = abs(-34) | ||
expect(absOfNegativeNumber).toBe(34) | ||
}) | ||
|
||
it('should return an absolute value of a positive number', () => { | ||
const absOfPositiveNumber = absVal(50) | ||
const absOfPositiveNumber = abs(50) | ||
expect(absOfPositiveNumber).toBe(50) | ||
}) | ||
|
||
it('should return an absolute value of a zero number', () => { | ||
const absOfPositiveNumber = absVal(0) | ||
const absOfPositiveNumber = abs(0) | ||
expect(absOfPositiveNumber).toBe(0) | ||
}) | ||
|
||
it('should return an absolute value of any floating number', () => { | ||
const absOfPositiveNumber = abs(-20.2034) | ||
expect(absOfPositiveNumber).toBe(20.2034) | ||
}) | ||
}) |
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.