| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,23 +53,16 @@ isEven: true | |||
| 53 | 53 | ||
| 54 | 54 | #### isPositive | |
| 55 | 55 | ||
| 56 | - This method determines if the number provided is positive. | ||
| 57 | - It is based on the fact that all positive numbers have their last | ||
| 58 | - left bit to be set to 0. However, if the number provided is zero | ||
| 59 | - or negative zero, it should still return false. | ||
| 56 | + This method determines if the number is positive. It is based on the fact that all positive | ||
| 57 | + numbers have their leftmost bit to be set to `0`. However, if the number provided is zero | ||
| 58 | + or negative zero, it should still return `false`. | ||
| 60 | 59 | ||
| 61 | 60 | ```text | |
| 62 | 61 | Number: 1 = 0b0001 | |
| 63 | 62 | isPositive: true | |
| 64 | 63 | ||
| 65 | 64 | Number: -1 = -0b0001 | |
| 66 | 65 | isPositive: false | |
| 67 | - | ||
| 68 | - Number: 0 = 0b0000 | ||
| 69 | - isPositive: false | ||
| 70 | - | ||
| 71 | - Number: -0 = 0b0000 | ||
| 72 | - isPositive: false | ||
| 73 | 66 | ``` | |
| 74 | 67 | ||
| 75 | 68 | > See [isPositive.js](isPositive.js) for further details. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,9 +2,18 @@ import isPositive from '../isPositive'; | |||
| 2 | 2 | ||
| 3 | 3 | describe('isPositive', () => { | |
| 4 | 4 | it('should detect if a number is positive', () => { | |
| 5 | + expect(isPositive(1)).toBe(true); | ||
| 6 | + expect(isPositive(2)).toBe(true); | ||
| 7 | + expect(isPositive(3)).toBe(true); | ||
| 8 | + expect(isPositive(5665)).toBe(true); | ||
| 9 | + expect(isPositive(56644325)).toBe(true); | ||
| 10 | + | ||
| 5 | 11 | expect(isPositive(0)).toBe(false); | |
| 6 | 12 | expect(isPositive(-0)).toBe(false); | |
| 7 | - expect(isPositive(1)).toBe(true); | ||
| 8 | 13 | expect(isPositive(-1)).toBe(false); | |
| 14 | + expect(isPositive(-2)).toBe(false); | ||
| 15 | + expect(isPositive(-126)).toBe(false); | ||
| 16 | + expect(isPositive(-5665)).toBe(false); | ||
| 17 | + expect(isPositive(-56644325)).toBe(false); | ||
| 9 | 18 | }); | |
| 10 | 19 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,13 @@ | |||
| 1 | 1 | /** | |
| 2 | - * @param {number} number | ||
| 2 | + * @param {number} number - 32-bit integer. | ||
| 3 | 3 | * @return {boolean} | |
| 4 | 4 | */ | |
| 5 | 5 | export default function isPositive(number) { | |
| 6 | - // Zero is neither a positive nor a negative number | ||
| 6 | + // Zero is neither a positive nor a negative number. | ||
| 7 | 7 | if (number === 0) { | |
| 8 | 8 | return false; | |
| 9 | 9 | } | |
| 10 | 10 | ||
| 11 | - // The most signification bit can be used to determine whether . | ||
| 11 | + // The most significant 32nd bit can be used to determine whether the number is positive. | ||
| 12 | 12 | return ((number >> 31) & 1) === 0; | |
| 13 | 13 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments