Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added three new functions squared, pow and factorial #32

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Binary file added images/calculator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 68 additions & 19 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Calculator</title>
<link href="styles.css" rel="stylesheet">
<link rel="icon" type="image/x-icon" href="images/calculator.png">
<script src="script.js" defer></script>
</head>
<body>
Expand All @@ -14,24 +15,72 @@
<div data-previous-operand class="previous-operand"></div>
<div data-current-operand class="current-operand"></div>
</div>
<button data-all-clear class="span-two">AC</button>
<button data-delete>DEL</button>
<button data-operation>÷</button>
<button data-number>1</button>
<button data-number>2</button>
<button data-number>3</button>
<button data-operation>*</button>
<button data-number>4</button>
<button data-number>5</button>
<button data-number>6</button>
<button data-operation>+</button>
<button data-number>7</button>
<button data-number>8</button>
<button data-number>9</button>
<button data-operation>-</button>
<button data-number>.</button>
<button data-number>0</button>
<button data-equals class="span-two">=</button>
<button data-all-clear class="span-two">
AC
</button>
<button data-delete>
DEL
</button>
<button data-operation>
%
</button>
<button data-operation>
!
</button>
<button data-operation>
&Sqrt;
</button>
<button data-operation>
&sup2;
</button>
<button data-operation>
÷
</button>
<button data-number>
1
</button>
<button data-number>
2
</button>
<button data-number>
3
</button>
<button data-operation>
*
</button>
<button data-number>
4
</button>
<button data-number>
5
</button>
<button data-number>
6
</button>
<button data-operation>
+
</button>
<button data-number>
7
</button>
<button data-number>
8
</button>
<button data-number>
9
</button>
<button data-operation>
-
</button>
<button data-number>
.
</button>
<button data-number>
0
</button>
<button data-equals class="span-two">
=
</button>
</div>
</body>
</html>
</html>
34 changes: 31 additions & 3 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,25 @@ class Calculator {
if (this.currentOperand === '') return
if (this.previousOperand !== '') {
this.compute()
}
}
this.operation = operation
this.previousOperand = this.currentOperand
this.currentOperand = ''
}

compute() {
let indicator = 0;
if(aSpecialOperations.indexOf(this.operation)> -1){
indicator = 1;
}
let computation
const prev = parseFloat(this.previousOperand)
const current = parseFloat(this.currentOperand)
if (isNaN(prev) || isNaN(current)) return
if(!indicator){
if ((isNaN(prev)) || isNaN(current)) return
}else{
if (isNaN(prev)) return
}
switch (this.operation) {
case '+':
computation = prev + current
Expand All @@ -47,6 +55,21 @@ class Calculator {
break
case '÷':
computation = prev / current
case '%':
computation = prev * current / 100
break
case '!':
let result = 1;
for(let i=1; i <= prev; i++){
result = result * i;
}
computation = result
break
case '²':
computation = prev * prev
break
case '√':
computation = Math.round(Math.sqrt(prev) * roundNumbers) / roundNumbers
break
default:
return
Expand Down Expand Up @@ -82,6 +105,9 @@ class Calculator {
} else {
this.previousOperandTextElement.innerText = ''
}
if(aSpecialOperations.indexOf(this.operation)> -1){
document.querySelector('[data-equals]').dispatchEvent(new Event("click"));
}
}
}

Expand All @@ -93,6 +119,8 @@ const deleteButton = document.querySelector('[data-delete]')
const allClearButton = document.querySelector('[data-all-clear]')
const previousOperandTextElement = document.querySelector('[data-previous-operand]')
const currentOperandTextElement = document.querySelector('[data-current-operand]')
const roundNumbers = 100000
const aSpecialOperations = ['!', '√', '²'];

const calculator = new Calculator(previousOperandTextElement, currentOperandTextElement)

Expand Down Expand Up @@ -123,4 +151,4 @@ allClearButton.addEventListener('click', button => {
deleteButton.addEventListener('click', button => {
calculator.delete()
calculator.updateDisplay()
})
})