You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
4.3 KiB

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <script src="https://cdn.tailwindcss.com"></script>
  7. <script src="//unpkg.com/alpinejs" defer></script>
  8. <title>Calculator</title>
  9. <script>
  10. document.addEventListener('alpine:init', () => {
  11. Alpine.data('calculator', () => ({
  12. result: 'Ready',
  13. current: 0,
  14. previous: 0,
  15. operator: '=',
  16. logAll(message = 'LOG') {
  17. console.log(`${message}: ${this.previous} ${this.operator} ${this.current} => ${this.result}`)
  18. },
  19. pressDigit(key) {
  20. this.current = this.current * 10 + key
  21. this.result = this.current
  22. if (this.operator == '=') { this.previous = 0 }
  23. this.logAll()
  24. },
  25. pressPlus() {
  26. this.operator = '+'
  27. this.compute()
  28. this.result = this.operator
  29. this.logAll()
  30. },
  31. pressMinus() {
  32. this.operator = '-'
  33. this.compute()
  34. this.result = this.operator
  35. this.logAll()
  36. },
  37. pressEqual() {
  38. this.compute()
  39. this.operator = '='
  40. this.result = `= ${this.previous}`
  41. this.previous = 0
  42. this.logAll()
  43. },
  44. compute() {
  45. this.logAll('BEFORE')
  46. switch (this.operator) {
  47. case '+':
  48. this.previous = this.previous + this.current
  49. break;
  50. case '-':
  51. this.previous = this.previous - this.current
  52. break;
  53. default:
  54. this.result = this.current
  55. this.previous = this.current
  56. }
  57. this.current = 0
  58. }
  59. }))
  60. console.log('Ready!')
  61. })
  62. </script>
  63. </head>
  64. <body class="bg-zinc-400 flex h-screen justify-center items-start " x-data="calculator">
  65. <div id='calculator' class="bg-slate-800 w-screen grid grid-cols-[3fr,1fr] h-screen ">
  66. <div id="screen" x-text="result"
  67. class="hover:ring-4 ring-blue-400 my-4 mx-2 p-8 text-right text-[8vh] text-white bg-black col-span-4 row-span-4 h-[20vh] rounded-xl border-double border-4 border-slate-800">
  68. </div>
  69. <div class="grid grid-cols-3 gap-3 mb-4">
  70. <template x-for="x in 3">
  71. <template x-for="y in 3">
  72. <button @click="pressDigit(x*3+y-3)"
  73. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 border-double border-4 border-slate-800 bg-slate-600 h-full"
  74. x-text="x*3+y-3">
  75. </button>
  76. </template>
  77. </template>
  78. <button @click="pressDigit(0)"
  79. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 col-span-2 bg-slate-600 border-double border-4 border-slate-800 h-full">0</button>
  80. <button
  81. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 bg-slate-600 border-double border-4 border-slate-800 h-full">.</button>
  82. </div>
  83. <div class="grid grid-rows-4 gap-3 mb-4">
  84. <!-- <button @click="pressMinus"
  85. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 border-double border-4 border-slate-600 bg-slate-900 text-white h-full">-</button> -->
  86. <button @click="pressPlus"
  87. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 border-double border-4 border-slate-600 bg-slate-900 text-white h-full row-span-2 ">+</button>
  88. <button @click="pressEqual"
  89. class="text-[10vh] rounded-2xl mx-2 active:bg-blue-400 hover:ring-4 ring-blue-400 border-double border-4 border-slate-600 bg-slate-900 text-white h-full row-span-2 ">=</button>
  90. </div>
  91. </div>
  92. </body>
  93. </html>