-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 627a9b1
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<title>Nathan's Calculator</title> | ||
<link rel="stylesheet" href="style.css"> | ||
</head> | ||
<body> | ||
<div class="calculator"> | ||
<div class="display"><input type="text" id="display" disabled></div> | ||
<div class="buttons"> | ||
<button onclick="clearDisplay()">C</button> | ||
<button onclick="pressButton('/')">/</button> | ||
<button onclick="pressButton('%')">%</button> | ||
<button onclick="pressButton('√')">√</button> | ||
<button onclick="pressButton('7')">7</button> | ||
<button onclick="pressButton('8')">8</button> | ||
<button onclick="pressButton('9')">9</button> | ||
<button onclick="pressButton('*')">*</button> | ||
<button onclick="pressButton('4')">4</button> | ||
<button onclick="pressButton('5')">5</button> | ||
<button onclick="pressButton('6')">6</button> | ||
<button onclick="pressButton('-')">-</button> | ||
<button onclick="pressButton('1')">1</button> | ||
<button onclick="pressButton('2')">2</button> | ||
<button onclick="pressButton('3')">3</button> | ||
<button onclick="pressButton('+')">+</button> | ||
<button onclick="pressButton('0')">0</button> | ||
<button onclick="pressButton('.')">.</button> | ||
<button onclick="calculateResult()">=</button> | ||
</div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var currentInput = ''; | ||
|
||
function pressButton(buttonValue) { | ||
var displayElement = document.getElementById('display'); | ||
|
||
if (['+', '-', '*', '/'].indexOf(buttonValue) !== -1) | ||
{ | ||
if (currentInput !== '' && ['+', '-', '*', '/'].indexOf(currentInput.slice(-1)) !== -1) | ||
{ | ||
currentInput = currentInput.slice(0, -1) + buttonValue; | ||
} | ||
else | ||
{ | ||
currentInput += buttonValue; | ||
} | ||
} else if (buttonValue === '.' && (currentInput === '' || isNaN(currentInput.slice(-1)))) | ||
{ | ||
currentInput += '0.'; | ||
} | ||
else if (buttonValue === '.' && currentInput.includes('.') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('+') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('-') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('*') && !currentInput.slice(currentInput.lastIndexOf('.')).includes('/')) | ||
{ | ||
return; | ||
} | ||
else | ||
{ | ||
currentInput += buttonValue; | ||
} | ||
displayElement.value = currentInput; | ||
} | ||
|
||
function clearDisplay() | ||
{ | ||
currentInput = ''; | ||
document.getElementById('display').value = ''; | ||
} | ||
|
||
function calculateResult() { | ||
var displayElement = document.getElementById('display'); | ||
if (['+', '-', '*', '/'].indexOf(currentInput.slice(-1)) !== -1) | ||
{ | ||
currentInput = currentInput.slice(0, -1); | ||
} | ||
|
||
try | ||
{ | ||
currentInput = currentInput.replace(/√/g, 'Math.sqrt').replace(/%/g, '/100'); | ||
var result = eval(currentInput); | ||
if (result > 9999999999) | ||
{ | ||
throw new Error('Overflow'); | ||
} | ||
else if(result < -9999999999) | ||
{ | ||
throw new Error('Underflow'); | ||
} | ||
if (result.toString().indexOf('.') !== -1 && result.toString().split('.')[0].length > 10) | ||
{ | ||
throw new Error('Overflow'); | ||
} | ||
displayElement.value = result; | ||
currentInput = result.toString(); | ||
} | ||
catch (error) | ||
{ | ||
displayElement.value = error.message; | ||
currentInput = ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
background-color: #f0f0f0; | ||
} | ||
|
||
.calculator { | ||
width: 300px; | ||
border: 1px solid #888; | ||
background-color: #fff; | ||
} | ||
|
||
.display { | ||
background-color: #e0e0e0; | ||
padding: 10px; | ||
} | ||
|
||
.display input { | ||
width: 100%; | ||
border: none; | ||
background: none; | ||
text-align: right; | ||
font-size: 2em; | ||
} | ||
|
||
.buttons button { | ||
width: 20%; | ||
padding: 20px; | ||
border: 1px solid #888; | ||
font-size: 1.5em; | ||
background-color: #f9f9f9; | ||
} | ||
|
||
.buttons button:hover { | ||
background-color: #ddd; | ||
} |