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
1 change: 1 addition & 0 deletions calculator_ex_2025
Submodule calculator_ex_2025 added at c01587
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<button class="number" id="number-3">3</button>
</div>
<div class="row operations">
<!-- <button id="add">+</button>
<button id="add">+</button>
<button id="subtract">-</button>
<button id="multiply">*</button>
<button id="divide">/</button> -->
<button id="divide">/</button>
</div>
<div class="row last-row">
<button id="clear">C</button>
Expand Down
74 changes: 74 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
let display=document.querySelector('.display')
let x,y;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of more meaningful names? x and y are not really indicating what those variables are

let result;
let operator;

let plus = document.getElementById('add')
let subtract = document.getElementById('subtract')
let multiply = document.getElementById('multiply')
let divide = document.getElementById('divide')

plus.addEventListener('click', function(){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can target all of the operators, you will be able to use 1 function for all of them instead of 4 like now

operator='+';
display.textContent='+';
})
subtract.addEventListener('click', function(){
operator='-';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoded strings should be saved inside constants variables

display.textContent='-';
})
multiply.addEventListener('click', function(){
operator='*';
display.textContent='*';
})
divide.addEventListener('click', function(){
operator='/';
display.textContent='/';
})

let equal = document.querySelector('#equal')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save the variables on top of the file


let num = document.querySelectorAll('.number').forEach(btn=>{
btn.addEventListener('click', function(){
if(!operator){
x=Number(btn.textContent);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be NAN add a check for it

display.textContent=btn.textContent;
}
else{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With early return statement above you dont need this else

y=Number(btn.textContent);
display.textContent=btn.textContent;
}
})
})


equal.addEventListener('click', function(){
if(operator=='+'){
result=x+y;
display.textContent=result;
}
if(operator=='-'){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add else if to not check all of the ifs every time

result = x-y;
display.textContent=result;
}
if(operator=='*'){
result=x*y;
display.textContent=result;
}
if(operator=='/'){
result=x/y;
display.textContent=result;
}
operator='';
x=result;

})

let clear = document.getElementById('clear')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save this variable on top of this file

clear.addEventListener('click', function(){
x=0;
y=0;
operator='';
display.textContent=""
})


7 changes: 3 additions & 4 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ body {
text-align: right;
margin-bottom: 10px;
overflow-x: auto;
height: 95px;
}

.buttons {
Expand Down Expand Up @@ -81,7 +82,6 @@ button:active {
transform: translateY(0);
}

/* Operations Row Styling */
.operations button {
background-color: var(--op-bg);
color: var(--op-color);
Expand All @@ -92,14 +92,13 @@ button:active {
filter: brightness(110%);
}

/* Last Row Styling */
.last-row button {
background-color: #10b981; /* Green for equals/action */
background-color: #10b981;
color: white;
}

.last-row button:first-child {
background-color: #ef4444; /* Red for Clear */
background-color: #ef4444;
}

.last-row button:nth-child(2) {
Expand Down