-
Notifications
You must be signed in to change notification settings - Fork 25
New solution #25
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
base: main
Are you sure you want to change the base?
New solution #25
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| let display=document.querySelector('.display') | ||
| let x,y; | ||
| 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(){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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='-'; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be |
||
| display.textContent=btn.textContent; | ||
| } | ||
| else{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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=='-'){ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
| 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') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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="" | ||
| }) | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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?
xandyare not really indicating what those variables are