File tree 3 files changed +82
-0
lines changed
solution/1800-1899/1880.Check if Word Equals Summation of Two Words
3 files changed +82
-0
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,35 @@ private:
124
124
};
125
125
```
126
126
127
+ ### **JavaScript**
128
+
129
+ ```js
130
+ /**
131
+ * @param {string} firstWord
132
+ * @param {string} secondWord
133
+ * @param {string} targetWord
134
+ * @return {boolean}
135
+ */
136
+ var isSumEqual = function(firstWord, secondWord, targetWord) {
137
+ let carry = 0;
138
+ let n1 = firstWord.length, n2 = secondWord.length;
139
+ let n3 = targetWord.length;
140
+ for (let i = 0; i < n3; i++) {
141
+ let num1 = getNum(firstWord.charAt(n1 - 1 - i));
142
+ let num2 = getNum(secondWord.charAt(n2 - 1 - i));
143
+ let sum = carry + num1 + num2;
144
+ if (getNum(targetWord.charAt(n3 - 1 - i)) != (sum % 10)) return false;
145
+ carry = parseInt(sum / 10);
146
+ }
147
+ return true;
148
+ };
149
+
150
+ function getNum (char) {
151
+ if (!char) return 0;
152
+ return char.charCodeAt() - 'a'.charCodeAt();
153
+ }
154
+ ```
155
+
127
156
### ** ...**
128
157
129
158
```
Original file line number Diff line number Diff line change @@ -144,6 +144,35 @@ private:
144
144
};
145
145
```
146
146
147
+ ### **JavaScript**
148
+
149
+ ```js
150
+ /**
151
+ * @param {string} firstWord
152
+ * @param {string} secondWord
153
+ * @param {string} targetWord
154
+ * @return {boolean}
155
+ */
156
+ var isSumEqual = function(firstWord, secondWord, targetWord) {
157
+ let carry = 0;
158
+ let n1 = firstWord.length, n2 = secondWord.length;
159
+ let n3 = targetWord.length;
160
+ for (let i = 0; i < n3; i++) {
161
+ let num1 = getNum(firstWord.charAt(n1 - 1 - i));
162
+ let num2 = getNum(secondWord.charAt(n2 - 1 - i));
163
+ let sum = carry + num1 + num2;
164
+ if (getNum(targetWord.charAt(n3 - 1 - i)) != (sum % 10)) return false;
165
+ carry = parseInt(sum / 10);
166
+ }
167
+ return true;
168
+ };
169
+
170
+ function getNum (char) {
171
+ if (!char) return 0;
172
+ return char.charCodeAt() - 'a'.charCodeAt();
173
+ }
174
+ ```
175
+
147
176
### ** ...**
148
177
149
178
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } firstWord
3
+ * @param {string } secondWord
4
+ * @param {string } targetWord
5
+ * @return {boolean }
6
+ */
7
+ var isSumEqual = function ( firstWord , secondWord , targetWord ) {
8
+ let carry = 0 ;
9
+ let n1 = firstWord . length , n2 = secondWord . length ;
10
+ let n3 = targetWord . length ;
11
+ for ( let i = 0 ; i < n3 ; i ++ ) {
12
+ let num1 = getNum ( firstWord . charAt ( n1 - 1 - i ) ) ;
13
+ let num2 = getNum ( secondWord . charAt ( n2 - 1 - i ) ) ;
14
+ let sum = carry + num1 + num2 ;
15
+ if ( getNum ( targetWord . charAt ( n3 - 1 - i ) ) != ( sum % 10 ) ) return false ;
16
+ carry = parseInt ( sum / 10 ) ;
17
+ }
18
+ return true ;
19
+ } ;
20
+
21
+ function getNum ( char ) {
22
+ if ( ! char ) return 0 ;
23
+ return char . charCodeAt ( ) - 'a' . charCodeAt ( ) ;
24
+ }
You can’t perform that action at this time.
0 commit comments