File tree 3 files changed +139
-0
lines changed
solution/0500-0599/0592.Fraction Addition and Subtraction
3 files changed +139
-0
lines changed Original file line number Diff line number Diff line change @@ -174,6 +174,54 @@ func gcd(a, b int) int {
174
174
}
175
175
```
176
176
177
+ #### JavaScript
178
+
179
+ ``` js
180
+ /**
181
+ * @param {string} expression
182
+ * @return {string}
183
+ */
184
+ var fractionAddition = function (expression ) {
185
+ let x = 0 ,
186
+ y = 1 ;
187
+
188
+ if (! expression .startsWith (' -' ) && ! expression .startsWith (' +' )) {
189
+ expression = ' +' + expression;
190
+ }
191
+
192
+ let i = 0 ;
193
+ const n = expression .length ;
194
+
195
+ while (i < n) {
196
+ const sign = expression[i] === ' -' ? - 1 : 1 ;
197
+ i++ ;
198
+
199
+ let j = i;
200
+ while (j < n && expression[j] !== ' +' && expression[j] !== ' -' ) {
201
+ j++ ;
202
+ }
203
+
204
+ const [a , b ] = expression .slice (i, j).split (' /' ).map (Number );
205
+ x = x * b + sign * a * y;
206
+ y *= b;
207
+ i = j;
208
+ }
209
+
210
+ const gcd = (a , b ) => {
211
+ while (b !== 0 ) {
212
+ [a, b] = [b, a % b];
213
+ }
214
+ return Math .abs (a);
215
+ };
216
+
217
+ const z = gcd (x, y);
218
+ x = Math .floor (x / z);
219
+ y = Math .floor (y / z);
220
+
221
+ return ` ${ x} /${ y} ` ;
222
+ };
223
+ ```
224
+
177
225
<!-- tabs:end -->
178
226
179
227
<!-- solution:end -->
Original file line number Diff line number Diff line change @@ -172,6 +172,54 @@ func gcd(a, b int) int {
172
172
}
173
173
```
174
174
175
+ #### JavaScript
176
+
177
+ ``` js
178
+ /**
179
+ * @param {string} expression
180
+ * @return {string}
181
+ */
182
+ var fractionAddition = function (expression ) {
183
+ let x = 0 ,
184
+ y = 1 ;
185
+
186
+ if (! expression .startsWith (' -' ) && ! expression .startsWith (' +' )) {
187
+ expression = ' +' + expression;
188
+ }
189
+
190
+ let i = 0 ;
191
+ const n = expression .length ;
192
+
193
+ while (i < n) {
194
+ const sign = expression[i] === ' -' ? - 1 : 1 ;
195
+ i++ ;
196
+
197
+ let j = i;
198
+ while (j < n && expression[j] !== ' +' && expression[j] !== ' -' ) {
199
+ j++ ;
200
+ }
201
+
202
+ const [a , b ] = expression .slice (i, j).split (' /' ).map (Number );
203
+ x = x * b + sign * a * y;
204
+ y *= b;
205
+ i = j;
206
+ }
207
+
208
+ const gcd = (a , b ) => {
209
+ while (b !== 0 ) {
210
+ [a, b] = [b, a % b];
211
+ }
212
+ return Math .abs (a);
213
+ };
214
+
215
+ const z = gcd (x, y);
216
+ x = Math .floor (x / z);
217
+ y = Math .floor (y / z);
218
+
219
+ return ` ${ x} /${ y} ` ;
220
+ };
221
+ ```
222
+
175
223
<!-- tabs:end -->
176
224
177
225
<!-- solution:end -->
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } expression
3
+ * @return {string }
4
+ */
5
+ var fractionAddition = function ( expression ) {
6
+ let x = 0 ,
7
+ y = 1 ;
8
+
9
+ if ( ! expression . startsWith ( '-' ) && ! expression . startsWith ( '+' ) ) {
10
+ expression = '+' + expression ;
11
+ }
12
+
13
+ let i = 0 ;
14
+ const n = expression . length ;
15
+
16
+ while ( i < n ) {
17
+ const sign = expression [ i ] === '-' ? - 1 : 1 ;
18
+ i ++ ;
19
+
20
+ let j = i ;
21
+ while ( j < n && expression [ j ] !== '+' && expression [ j ] !== '-' ) {
22
+ j ++ ;
23
+ }
24
+
25
+ const [ a , b ] = expression . slice ( i , j ) . split ( '/' ) . map ( Number ) ;
26
+ x = x * b + sign * a * y ;
27
+ y *= b ;
28
+ i = j ;
29
+ }
30
+
31
+ const gcd = ( a , b ) => {
32
+ while ( b !== 0 ) {
33
+ [ a , b ] = [ b , a % b ] ;
34
+ }
35
+ return Math . abs ( a ) ;
36
+ } ;
37
+
38
+ const z = gcd ( x , y ) ;
39
+ x = Math . floor ( x / z ) ;
40
+ y = Math . floor ( y / z ) ;
41
+
42
+ return `${ x } /${ y } ` ;
43
+ } ;
You can’t perform that action at this time.
0 commit comments