From 531efa364899797862b960396aae58cb5cfce6b8 Mon Sep 17 00:00:00 2001 From: se0jinYoon Date: Wed, 8 Nov 2023 05:44:59 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20header=20=EC=9D=B4=EB=A6=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index d422693..e3a2ae0 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@
-

혜인이의 계산기

+

서진이의 계산기

From c84240a9b3ac41659e2a39f0b489bef1d03e83f1 Mon Sep 17 00:00:00 2001 From: se0jinYoon Date: Wed, 8 Nov 2023 06:01:54 +0900 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 10 +++++----- src/main.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index e3a2ae0..e2bc505 100644 --- a/index.html +++ b/index.html @@ -13,18 +13,18 @@

서진이의 계산기

- - + - +

=

-

정답

+

정답

- +
diff --git a/src/main.ts b/src/main.ts index e69de29..3345315 100644 --- a/src/main.ts +++ b/src/main.ts @@ -0,0 +1,38 @@ +const userInput = document.getElementsByClassName('userInput'); +const operationType: HTMLSelectElement = document.getElementById('operationType') as HTMLSelectElement; +const answer: HTMLElement = document.getElementById('answer')!; + + +const onClickResult = () => { + const firstInput: number = Number(userInput[0]); + const secondInput:number = Number(userInput[1]); + 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; +} + From e9799852528af8c49be994c255083676e3e50d2c Mon Sep 17 00:00:00 2001 From: se0jinYoon Date: Wed, 8 Nov 2023 06:18:50 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20click=20=EC=9D=B4=EB=B2=A4=ED=8A=B8?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20=EB=B3=80=EA=B2=BD,=20input=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=95=84=EC=9D=B4=EB=94=94=20=EA=B0=92=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=91=EA=B7=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.html | 8 ++++---- src/main.js | 36 ++++++++++++++++++++++++++++++++++++ src/main.ts | 13 +++++++------ 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 src/main.js diff --git a/index.html b/index.html index e2bc505..e06e50a 100644 --- a/index.html +++ b/index.html @@ -13,19 +13,19 @@

서진이의 계산기

- + - +

=

정답

- +
- + 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 3345315..2c335c4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,16 +1,16 @@ -const userInput = document.getElementsByClassName('userInput'); +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; - -const onClickResult = () => { - const firstInput: number = Number(userInput[0]); - const secondInput:number = Number(userInput[1]); +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 { @@ -36,3 +36,4 @@ function calculate(firstInput:number, secondInput:number, operator:string):numbe return result; } +resultBtn.addEventListener("click", getResult); \ No newline at end of file