diff --git a/index.html b/index.html index d422693..e06e50a 100644 --- a/index.html +++ b/index.html @@ -9,23 +9,23 @@
-

혜인이의 계산기

+

서진이의 계산기

- - + - +

=

-

정답

+

정답

- +
- + diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..a9c9ff6 --- /dev/null +++ b/src/main.js @@ -0,0 +1,36 @@ +var userInput1 = document.getElementById('userInput1'); +var userInput2 = document.getElementById('userInput2'); +var operationType = document.getElementById('operationType'); +var answer = document.getElementById('answer'); +var resultBtn = document.getElementById('resultBtn'); +function getResult() { + var firstInput = Number(userInput1.value); + var secondInput = Number(userInput2.value); + var operator = operationType.value; + var result = calculate(firstInput, secondInput, operator); + answer.innerText = result.toString(); +} +function calculate(firstInput, secondInput, operator) { + var result = 0; + switch (operator) { + case "sum": + result = firstInput + secondInput; + break; + case "sub": + result = firstInput - secondInput; + break; + case "mul": + result = firstInput * secondInput; + break; + case "divide": + if (secondInput !== 0) { + result = firstInput / secondInput; + } + else { + throw new Error('0으로 나눌 수 없습니다 ^0^'); + } + break; + } + return result; +} +resultBtn.addEventListener("click", getResult); diff --git a/src/main.ts b/src/main.ts index e69de29..2c335c4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -0,0 +1,39 @@ +const userInput1: HTMLInputElement = document.getElementById('userInput1') as HTMLInputElement; +const userInput2: HTMLInputElement = document.getElementById('userInput2') as HTMLInputElement; +const operationType: HTMLSelectElement = document.getElementById('operationType') as HTMLSelectElement; +const answer: HTMLElement = document.getElementById('answer')!; +const resultBtn: HTMLButtonElement = document.getElementById('resultBtn') as HTMLButtonElement; + +function getResult():void { + const firstInput: number = Number(userInput1.value); + const secondInput:number = Number(userInput2.value); + const operator:string = operationType.value; + + const result:number = calculate(firstInput, secondInput, operator); + answer.innerText = result.toString(); +} + +function calculate(firstInput:number, secondInput:number, operator:string):number { + let result:number = 0; + switch(operator) { + case "sum": + result = firstInput + secondInput; + break; + case "sub": + result = firstInput - secondInput; + break; + case "mul": + result = firstInput * secondInput; + break; + case "divide": + if (secondInput !== 0) { + result = firstInput / secondInput; + } else { + throw new Error('0으로 나눌 수 없습니다 ^0^'); + } + break; + } + return result; +} + +resultBtn.addEventListener("click", getResult); \ No newline at end of file