Skip to content

Instantly share code, notes, and snippets.

@njabulomajozi
Created April 22, 2020 16:51
Show Gist options
  • Save njabulomajozi/aa35f2005047b09a563da29286936481 to your computer and use it in GitHub Desktop.
Save njabulomajozi/aa35f2005047b09a563da29286936481 to your computer and use it in GitHub Desktop.
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