-
Notifications
You must be signed in to change notification settings - Fork 0
/
remainCash.js
50 lines (41 loc) · 1.44 KB
/
remainCash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const mileElement = document.getElementById("mile");
const priceElement = document.getElementById("price");
const submitElement = document.getElementById("submitButton");
submitElement.addEventListener("click", calculate);
function calculate(event) {
// console.log("t");
event.preventDefault();
const mile = parseInt(mileElement.value);
const price = parseInt(priceElement.value);
const contentElement = document.getElementById("content");
let count = 1;
let flag = 1;
while (flag) {
count++;
for (const usedMile of Array(mile).keys()) {
const usedMoney = price - usedMile;
const gotMile = usedMoney * (5 / 100);
const resultMile = mile - usedMile + gotMile;
const usedMoneyZeroCnt = usedMoney.toString().length - 1;
const precision = 10 ** usedMoneyZeroCnt / 10 ** count;
if (precision < 10) {
flag = 0;
contentElement.innerHTML = `
<p>1원 단위가 나옵니다. 차라리 전액 현금 결제하십시오.</p>
`;
break;
}
const isDivisable = resultMile % precision === 0;
const isGTzero = resultMile >= 0;
if (isDivisable && isGTzero) {
contentElement.innerHTML = `
<p id="usedMile">사용할 캐시백 : ${usedMile} </p>
<p>사용할 현금 :${usedMoney}</p>
<p>최종 캐시백 : ${resultMile}</p>
`;
flag = 0;
break;
}
}
}
}