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
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>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider to create these elements in code. Use forEach to do it.

</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
42 changes: 42 additions & 0 deletions script.js

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

consider to define the function before the listener definition

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const display=document.querySelector('.display');
display.style.minHeight = '100px';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use css file to define the style

display.innerText = '';
const numberButtons=document.querySelectorAll('.number');
numberButtons.forEach(button=> {
button.addEventListener('click',function() {
display.innerText=display.innerText+button.innerText;
});
});

let selectedOperator = '';
const opButtons=document.querySelectorAll('.operations button');
opButtons.forEach(op =>{
op.addEventListener('click',function(){
selectedOperator = op.innerText;
display.innerText=display.innerText+selectedOperator;
});
});

const equalButton=document.querySelector('#equal');
equalButton.addEventListener('click',function(){

const parts=display.innerText.split(selectedOperator);

const num1=Number(parts[0]);// Number הופך טקסט למספר
const num2=Number(parts[1]);

if(selectedOperator==='+')
display.innerText=num1+num2;
else if(selectedOperator==='-')
display.innerText=num1-num2;
else if(selectedOperator==='*')
display.innerText=num1*num2;
else if(selectedOperator==='/')
display.innerText=num1/num2;
});

const clearButton=document.querySelector('#clear');
clearButton.addEventListener('click',function(){
display.innerText='';
selectedOperator='';
});