回答編集履歴

3

書式の追加

2020/08/03 15:51

投稿

Jon_do
Jon_do

スコア1373

test CHANGED
@@ -64,10 +64,232 @@
64
64
 
65
65
  ```
66
66
 
67
- 元のコードだと少数の計算をするとずれてたので対応する予定。
67
+ 数の計算などの処理追加
68
+
69
+ なんだか酷いことに
68
70
 
69
71
  ```Javascript
70
72
 
71
-
73
+ const first = document.getElementById("q6-1");
74
+
75
+ const second = document.getElementById("q6-2");
76
+
77
+ const btn = document.getElementById("q6-btn");
78
+
79
+ const result = document.getElementById("q6result");
80
+
81
+
82
+
83
+ btn.addEventListener("click", () => {
84
+
85
+ const fVal = first.value;
86
+
87
+ const sVal = second.value;
88
+
89
+ const fNum = Number(fVal);
90
+
91
+ const sNum = Number(sVal);
92
+
93
+ if (fVal.match(/^-?[0-9]+.[0-9]+$/) || sVal.match(/^-?[0-9]+.[0-9]+$/)) {
94
+
95
+ changeInteger(fVal, sVal);
96
+
97
+ }
98
+
99
+ else if (Number.isInteger(fNum) && Number.isInteger(sNum)) {
100
+
101
+ result.innerText = calc(fNum, sNum);
102
+
103
+ } else {
104
+
105
+ result.innerText = "エラー";
106
+
107
+ }
108
+
109
+ });
110
+
111
+
112
+
113
+ const calc = (num1, num2) => {
114
+
115
+ const select = document.getElementById("op6").value;
116
+
117
+ switch (select) {
118
+
119
+ case "+":
120
+
121
+ return num1 + num2;
122
+
123
+ break;
124
+
125
+ case "‐":
126
+
127
+ return num1 - num2;
128
+
129
+ break;
130
+
131
+ case "×":
132
+
133
+ return num1 * num2;
134
+
135
+ console.log(num1 * num2);
136
+
137
+ break;
138
+
139
+ case "÷":
140
+
141
+ if (num2 === 0) { return "エラー" }
142
+
143
+ else {
144
+
145
+ return num1 * 10000 % num2 === 0 ? num1 / num2
146
+
147
+ : Math.trunc(num1 / num2) + "余り" + num1 % num2;
148
+
149
+ }
150
+
151
+ break;
152
+
153
+ }
154
+
155
+ }
156
+
157
+
158
+
159
+ const changeInteger = (num1, num2) => {
160
+
161
+ const num1DotLen = num1.indexOf(".") === -1 ? 0 : num1.split(".")[1].length;
162
+
163
+ const num2DotLen = num2.indexOf(".") === -1 ? 0 : num2.split(".")[1].length;
164
+
165
+ const num1Rep = num1.indexOf(".") === -1 ? num1 : num1.replace(".", "");
166
+
167
+ const num2Rep = num2.indexOf(".") === -1 ? num2 : num2.replace(".", "");
168
+
169
+ if (num1DotLen > num2DotLen) {
170
+
171
+ const digit = num1DotLen - num2DotLen;
172
+
173
+ const num1 = addZero(0, num1Rep);
174
+
175
+ const num2 = addZero(digit, num2Rep);
176
+
177
+ changeDecimal(calc(num1, num2), num1DotLen, num2DotLen);
178
+
179
+ } else if (num2DotLen > num1DotLen) {
180
+
181
+ const digit = num2DotLen - num1DotLen;
182
+
183
+ const num1 = addZero(digit, num1Rep)
184
+
185
+ const num2 = addZero(0, num2Rep);
186
+
187
+ changeDecimal(calc(num1, num2), num2DotLen, num1DotLen);
188
+
189
+ } else {
190
+
191
+ const num1 = addZero(0, num1Rep);
192
+
193
+ const num2 = addZero(0, num2Rep);
194
+
195
+ changeDecimal(calc(num1, num2), num1DotLen, num1DotLen);
196
+
197
+ }
198
+
199
+ }
200
+
201
+
202
+
203
+ const addZero = (digit, num) => {
204
+
205
+ const zero = "0"
206
+
207
+ const number = Number(num + zero.repeat(digit));
208
+
209
+ return number;
210
+
211
+ }
212
+
213
+
214
+
215
+ const changeDecimal = (num, dot1, dot2) => {
216
+
217
+ const select = document.getElementById("op6").value;
218
+
219
+ if (select === "+" || select === "‐") {
220
+
221
+ const answer = String(num).slice(0, String(num).length - dot1) + "." + String(num).slice(String(num).length - dot1);
222
+
223
+ if (answer.slice(0, 1) === ".") {
224
+
225
+ result.innerText = 0 + answer
226
+
227
+ } else if (answer.slice(0, 2) === "-.") {
228
+
229
+ result.innerText = answer.slice(0, 1) + "0" + answer.slice(1, answer.length)
230
+
231
+ } else {
232
+
233
+ result.innerText = answer
234
+
235
+ }
236
+
237
+ }
238
+
239
+ if (select === "×") {
240
+
241
+ const zero = "0";
242
+
243
+ const add0 = String(num).match("-")
244
+
245
+ ? "-" + zero.repeat(dot1 + dot2) + String(num).replace("-", "")
246
+
247
+ : zero.repeat(dot1 + dot2) + String(num);
248
+
249
+ if (add0.match(/[0]*$/)) {
250
+
251
+ const reg = new RegExp("[0]" + "{" + (dot1 - dot2) + "}$", "g");
252
+
253
+ const regNum = add0.replace(reg, "");
254
+
255
+ const answer = regNum.slice(0, regNum.length - (dot1 + dot2)) + "." + regNum.slice(regNum.length - (dot1 + dot2))
256
+
257
+ result.innerText = Number(answer);
258
+
259
+ }
260
+
261
+ }
262
+
263
+ if (select === "÷") {
264
+
265
+ if (String(num).indexOf("余り") === -1) {
266
+
267
+ result.innerText = num;
268
+
269
+ } else {
270
+
271
+ const remainder = String(num).split("余り")[1];
272
+
273
+ const remainderDot = remainder.slice(0, remainder.length - dot1) + "." + remainder.slice(remainder.length - dot1);
274
+
275
+ if (remainderDot.slice(0, 1) === ".") {
276
+
277
+ result.innerText = String(num).split("余り")[0] + "余り" + 0 + remainderDot;
278
+
279
+ } else if (remainderDot.slice(0, 2) === "-.") {
280
+
281
+ result.innerText = String(num).split("余り")[0] + "余り" + remainderDot.slice(0, 1) + "0" + remainderDot.slice(1, remainderDot.length)
282
+
283
+ } else {
284
+
285
+ result.innerText = String(num).split("余り")[0] + "余り" + remainderDot;
286
+
287
+ }
288
+
289
+ }
290
+
291
+ }
292
+
293
+ }
72
294
 
73
295
  ```。

2

文法の修正

2020/08/03 15:50

投稿

Jon_do
Jon_do

スコア1373

test CHANGED
@@ -63,3 +63,11 @@
63
63
  });
64
64
 
65
65
  ```
66
+
67
+ 元のコードだと少数の計算をするとずれてたので対応する予定。
68
+
69
+ ```Javascript
70
+
71
+
72
+
73
+ ```。

1

書式の改善

2020/07/31 05:36

投稿

Jon_do
Jon_do

スコア1373

test CHANGED
@@ -1,6 +1,8 @@
1
1
  アウトプットのため作成。とりあえず
2
2
 
3
3
  半角数字以外(-.+は除く)を打ち込むとエラーが出るようにしてみました。
4
+
5
+ 2進数や8進数16進数も対応しているみたいです。
4
6
 
5
7
  ```Javascript
6
8