Created
April 22, 2020 16:51
-
-
Save njabulomajozi/aa35f2005047b09a563da29286936481 to your computer and use it in GitHub Desktop.
This file contains 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
function isZeroDivided(expression) { | |
if (typeof expression !== 'string') { | |
return false; | |
} | |
const isExpression = /(?:(?:^|[-+_*/])(?:\s*-?\d+(\.\d+)?(?:[eE][+-]?\d+)?\s*))+$/; | |
const isZeroDivision = /\/0/; | |
return isExpression.test(expression) ? isZeroDivision.test(expression) : false; | |
} | |
console.assert(isZeroDivided(2) === false); | |
console.assert(isZeroDivided('') === false); | |
console.assert(isZeroDivided('2+2') === false); | |
console.assert(isZeroDivided('2/0') === true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment