Simple Math & GCD
Breakdown of the Regular Expression: (/[+-]?\d+\/\d+/g)\n[+-]?:\n\n[+-]: This is a character class that matches either a + or a - sign.\n?: This quantifier mean
**Breakdown of the Regular Expression:** (/[+-]?\\d+\\/\\d+/g)\n[+-]?:\n\n[+-]: This is a character class that matches either a + or a - sign.\n?: This quantifier means "zero or one occurrence." It allows the sign to be optional, so it will match fractions with or without a sign.\n\\d+:\n\n\\d: This matches any digit from 0 to 9.\n+: This quantifier means "one or more occurrences." It ensures that the regular expression matches one or more digits, which forms the numerator of the fraction.\n\\/:\n\n\\/: This escapes the / character, which is used as the division symbol in fractions. The backslash (\\) is necessary because / has special meaning in regular expressions (it is typically used to delimit the regex pattern).\n\\d+:\n\nThis is the same as the previous \\d+ and matches one or more digits, which forms the denominator of the fraction.\ng:\n\nThe g flag stands for "global." It means that the regular expression will search for all matches in the string, not just the first one. Without this flag, the regular expression would stop after finding the first match.\nExample Matches:\nGiven the string "-1/2+1/2+1/3", here\'s how the regular expression would match:\n\n-1/2:\n[+-]?: Matches the -.\n\\d+: Matches 1.\n\\/: Matches the /.\n\\d+: Matches 2.\n\n+1/2:\n[+-]?: Matches the +.\n\\d+: Matches 1.\n\\/: Matches the /.\n\\d+: Matches 2.\n\n+1/3:\n[+-]?: Matches the +.\n\\d+: Matches 1.\n\\/: Matches the /.\n\\d+: Matches 3.\n\nSummary:\n[+-]?: Matches an optional sign.\n\\d+: Matches the numerator (one or more digits).\n\\/: Matches the division symbol /.\n\\d+: Matches the denominator (one or more digits).\ng: Ensures all fractions in the string are matched, not just the first one.\n\n\n# Code\n```javascript []\n/**\n * @param {string} expression\n * @return {string}\n */\nvar fractionAddition = function (expression) {\n    const fractions = expression.match(/[+-]?\\d+\\/\\d+/g); //[ \'-1/2\', \'+1/2\', \'-3/4\' ]\n\n    let numerator = 0\n    let denominator = 1\n\n    const gcd = (a, b) => b === 0 ? a : gcd(b, a % b)\n\n    for (const fract of fractions) {\n        const [num, denom] = fract.split(\'/\')//  \'-3/4\'  ->     [ \'-3\', \'4\' ]\n\n        numerator = numerator * denom + num * denominator\n        denominator *= denom\n\n\n        //simplify the fraction\n\n        const gcdval = gcd(Math.abs(numerator), Math.abs(denominator))\n        numerator /= gcdval\n        denominator /= gcdval\n\n    }\n    return `${numerator}/${denominator}`\n};\n```
['Math', 'Simulation', 'JavaScript']