Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"liveServer.settings.port": 5501
}
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<!-- <script src="main.js"></script> -->
<script src="main.js"></script>
<title>Document</title>
</head>
<body>
<form class="calculator">
<label for="first-number">First Number:</label>
<!-- the "onkeyup" event listener passes its input's "value" to the "saveFirstNumber" function in the main.js file -->
<input type="number" id="first-Number" name="first-Number" placeholder="type the first number" value="" onkeyup="saveFirstNumber(this.value)">
<input type="number" id="first-Number" name="first-Number" placeholder="type the first number" value="" onkeyup="saveFirstNumber(this.value)" oninput="saveFirstNumber(this.value)">
<label for="second-number">Second Number:</label>
<input type="number" id="second-Number" name="second-Number" placeholder="type the second number" onkeyup="saveSecondNumber(this.value)">
<div>
Expand Down
11 changes: 9 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ let operation = null

// this function takes in the number you type in the input field and saves it to the "firstNum" variable
const saveFirstNumber = (num) => {
firstNum = parseInt(num)
console.log('num:', num)
firstNum = parseInt(num)
}

// this function takes in the number you type in the 2nd input field and saves it to the "secondNum" variable
Expand Down Expand Up @@ -59,6 +60,12 @@ const putResultInElement = (operationResults) => {

// The function uses the value of "operation" variable to determine which operation function it should use on the number: add, subtract, multiply, divide, or modulus
const equals = () => {
if (!firstNum) {
firstNum = parseInt(document.getElementById('first-Number').value)
}
if (!secondNum) {
secondNum = parseInt(document.getElementById('second-Number').value)
}
switch (operation) {
case "addition": putResultInElement(add(firstNum, secondNum))
break;
Expand All @@ -70,7 +77,7 @@ const equals = () => {
break;
case "modulus": console.log(modulus(firstNum, secondNum))
break;
default: "Choose an operation"
default: alert("Choose an operation")
}
}