diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md b/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md index 9f6eccb8bf0d6..f3014f3f82008 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/README.md @@ -174,6 +174,54 @@ func gcd(a, b int) int { } ``` +#### JavaScript + +```js +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function (expression) { + let x = 0, + y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +}; +``` + diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md index f2217c40bb8c9..f7f073920fa37 100644 --- a/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/README_EN.md @@ -172,6 +172,54 @@ func gcd(a, b int) int { } ``` +#### JavaScript + +```js +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function (expression) { + let x = 0, + y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +}; +``` + diff --git a/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js new file mode 100644 index 0000000000000..67ec625258b22 --- /dev/null +++ b/solution/0500-0599/0592.Fraction Addition and Subtraction/Solution.js @@ -0,0 +1,43 @@ +/** + * @param {string} expression + * @return {string} + */ +var fractionAddition = function (expression) { + let x = 0, + y = 1; + + if (!expression.startsWith('-') && !expression.startsWith('+')) { + expression = '+' + expression; + } + + let i = 0; + const n = expression.length; + + while (i < n) { + const sign = expression[i] === '-' ? -1 : 1; + i++; + + let j = i; + while (j < n && expression[j] !== '+' && expression[j] !== '-') { + j++; + } + + const [a, b] = expression.slice(i, j).split('/').map(Number); + x = x * b + sign * a * y; + y *= b; + i = j; + } + + const gcd = (a, b) => { + while (b !== 0) { + [a, b] = [b, a % b]; + } + return Math.abs(a); + }; + + const z = gcd(x, y); + x = Math.floor(x / z); + y = Math.floor(y / z); + + return `${x}/${y}`; +};