خوب شکل بالا ماشین حساب ما رو نشون میده که در عین سادگی تقریبا کنترول شده و بی عیب کار می کنه.
بریم به قسمت کد ها
html <div class="conteaner"> <div id="shownumer"> <div id="sunelespans"> <span id="yek"></span> <span id="doo"></span> <span id="see"></span> </div> </div> <button id="plus">+</button> <button id="mines">-</button> <button id="multiple">x</button> <button id="division">/</button> <button id="clean">c</button> <button id="two">2</button> <button id="three">3</button> <button id="four">4</button> <button id="five">5</button> <button id="six">6</button> <button id="zero">0</button> <button id="one">1</button> <button id="seven">7</button> <button id="egiht">8</button> <button id="nine">9</button> <button id="equal">=</button>
css *{ font-size: xx-large;} .conteaner{ width: 50%; height: 500px; display: grid; grid-template-columns: 50px 50px 50px 50px 50px; gap: 20px; grid-auto-rows: 100px; margin-right: auto; margin-left: auto; } #shownumer{ background-color: rgb(3, 225, 66); grid-column: 1 / -1; grid-row: 1; font-size: 60px; text-align: center; } span{ font-size: 50px; } button#equal{ background-color: rgb(247, 0, 0); grid-column: 1 / -1; grid-row: auto;} button:not(#equal){ border-radius: 50%; background-color: black; color: aliceblue; } #sunelespans{ text-align: center; } #plus{ background-color: #af1a1aa1!important } #mines{ background-color: rgb(18 130 219) !important; } #multiple{ background-color: rgb(63, 35, 173) !important; } #division{ background-color: rgb(156, 93, 157) !important; } #clean{ background-color: rgb(199, 122, 22) !important; }
قسمت جاوااسکریپتیش
js const spanone=document.querySelector("#yek") const spanthree= document.querySelector("#see") const spancondition=document.querySelector("#doo") const one=document.getElementById("one") one.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(1) } else if(!spancondition.textContent==""){ spanthree.append(1) } }) const two=document.getElementById("two") two.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(2) } else if(!spancondition.textContent==""){ spanthree.append(2) } }) const three=document.getElementById("three") three.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(3) } else if(!spancondition.textContent==""){ spanthree.append(3) } }) const four=document.getElementById("four") four.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(4) } else if(!spancondition.textContent==""){ spanthree.append(4) } }) const five=document.getElementById("five") five.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(5) } else if(!spancondition.textContent==""){ spanthree.append(5) } }) const six=document.getElementById("six") six.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(6) } else if(!spancondition.textContent==""){ spanthree.append(6) } }) const seven=document.getElementById("seven") seven.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(7) } else if(!spancondition.textContent==""){ spanthree.append(7) } }) const eight=document.getElementById("egiht") eight.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(8) } else if(!spancondition.textContent==""){ spanthree.append(8) } }) const nine=document.getElementById("nine") nine.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(9) } else if(!spancondition.textContent==""){ spanthree.append(9) } }) const zero=document.getElementById("zero") zero.addEventListener("click",e=>{ if(spancondition.textContent==""){ yek.append(0) } else if(!spancondition.textContent==""){ spanthree.append(0) } }) const plus=document.querySelector("#plus") plus.addEventListener("click",e=>{ if(!spanone.textContent=="" && spancondition.textContent=="" ){ document.querySelector("#doo")=plus.textContent } else if(!spanone.textContent=="" && !spancondition.textContent=="" && !spanthree.textContent==""){ equall() document.querySelector("#doo")=plus.textContent } else if(!spanone.textContent=="" && !spancondition.textContent==""){ document.querySelector("#doo")=plus.textContent } }) const mines=document.querySelector("#mines") mines.addEventListener("click",e=>{ if(!spanone.textContent=="" && spancondition.textContent=="" ){ document.querySelector("#doo")=mines.textContent } else if(!spanone.textContent=="" && !spancondition.textContent=="" && !spanthree.textContent==""){ equall() document.querySelector("#doo")=mines.textContent }else if(!spanone.textContent=="" && !spancondition.textContent==""){ document.querySelector("#doo")=mines.textContent } }) const multiple=document.querySelector("#multiple") multiple.addEventListener("click",e=>{ if(!spanone.textContent=="" && spancondition.textContent=="" ){ document.querySelector("#doo")=multiple.textContent } else if(!spanone.textContent=="" && !spancondition.textContent=="" && !spanthree.textContent==""){ equall() document.querySelector("#doo")=multiple.textContent }else if(!spanone.textContent=="" && !spancondition.textContent==""){ document.querySelector("#doo")=multiple.textContent } }) const division=document.querySelector("#division") division.addEventListener("click",e=>{ if(!spanone.textContent=="" && spancondition.textContent=="" ){ document.querySelector("#doo")=division.textContent } else if(!spanone.textContent=="" && !spancondition.textContent=="" && !spanthree.textContent==""){ equall() document.querySelector("#doo")=division.textContent }else if(!spanone.textContent=="" && !spancondition.textContent==""){ document.querySelector("#doo")=division.textContent } }) function equall(){ const shownumer=document.querySelector("#shownumer") let firstvalue=document.querySelector("#yek") let firstvalueu=parseInt(firstvalue.textContent) const secondvalue=document.querySelector("#doo") let secondvalueu=secondvalue.textContent let thirdvalue=document.querySelector("#see") let thirdvalueu=parseInt(thirdvalue.textContent) switch(secondvalueu){ case "+" :firstvalue=firstvalueu+thirdvalueu , thirdvalue="",secondvalue="" break; case "-" :firstvalue=firstvalueu-thirdvalueu , thirdvalue="",secondvalue="" break; case "x" :firstvalue=firstvalueu*thirdvalueu , thirdvalue="",secondvalue="" break; case "/" :firstvalue=firstvalueu/thirdvalueu , thirdvalue="",secondvalue="" break; } } const equal=document.querySelector("#equal") equal.addEventListener("click",e=>{ equall()}) const clean=document.querySelector("#clean") clean.addEventListener("click",e=>{ let firstvalue=document.querySelector("#yek") const secondvalue=document.querySelector("#doo") let thirdvalue=document.querySelector("#see") firstvalue="" secondvalue="" thirdvalue="" })
ccc