Add 2 files
Browse files- index.html +39 -19
- main.js +78 -0
index.html
CHANGED
|
@@ -1,19 +1,39 @@
|
|
| 1 |
-
<
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html><head><link href="https://cdn.jsdelivr.net/npm/daisyui@3.1.6/dist/full.css" rel="stylesheet" type="text/css" /><script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script><script src="https://cdn.tailwindcss.com?plugins=forms,typography,aspect-ratio"></script><script defer src="https://cdnjs.cloudflare.com/ajax/libs/three.js/0.156.1/three.min.js"></script><script type="module" src="main.js"></script><title>Calculator</title></head><body>
|
| 2 |
+
|
| 3 |
+
<header class="p-4">
|
| 4 |
+
<h1 class="text-2xl font-bold">Calculator</h1>
|
| 5 |
+
<p class="text-gray-500">Use this calculator to perform basic arithmetic operations.</p>
|
| 6 |
+
</header>
|
| 7 |
+
|
| 8 |
+
<x-data="{ total: 0, newNum: '', selectedOperation: '', previousOperation: '', result: '' }`"></x-data>
|
| 9 |
+
<x-cloak>
|
| 10 |
+
<main class="px-8">
|
| 11 |
+
<div class="flex flex-col">
|
| 12 |
+
<form
|
| 13 |
+
x-init="function () {
|
| 14 |
+
calculateAndUpdateDisplay();
|
| 15 |
+
}"
|
| 16 |
+
>
|
| 17 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-html="display"></button>
|
| 18 |
+
|
| 19 |
+
<div class="flex flex-wrap">
|
| 20 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '1'">1</button>
|
| 21 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '2'">2</button>
|
| 22 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '3'">3</button>
|
| 23 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '+'">+</button>
|
| 24 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '-'">-</button>
|
| 25 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '*'">*</button>
|
| 26 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '/'">/</button>
|
| 27 |
+
</div>
|
| 28 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="clearAll()">AC</button>
|
| 29 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="clearNewNum()">CE</button>
|
| 30 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="addDecimal()">.</button>
|
| 31 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="calculateAndUpdateDisplay()">=</button>
|
| 32 |
+
</form>
|
| 33 |
+
</div>
|
| 34 |
+
</main>
|
| 35 |
+
</x-cloak>
|
| 36 |
+
|
| 37 |
+
</div>
|
| 38 |
+
|
| 39 |
+
</body></html>
|
main.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Define the application
|
| 2 |
+
const App = {
|
| 3 |
+
template: `<div class="flex flex-col h-screen">
|
| 4 |
+
<header class="p-4">
|
| 5 |
+
<h1 class="text-2xl font-bold">Calculator</h1>
|
| 6 |
+
<p class="text-gray-500">Use this calculator to perform basic arithmetic operations.</p>
|
| 7 |
+
</header>
|
| 8 |
+
|
| 9 |
+
<main class="px-8">
|
| 10 |
+
<div class="flex flex-wrap">
|
| 11 |
+
<calculator />
|
| 12 |
+
</div>
|
| 13 |
+
</main>
|
| 14 |
+
|
| 15 |
+
</div>`,
|
| 16 |
+
|
| 17 |
+
data() {
|
| 18 |
+
return {
|
| 19 |
+
display: 0,
|
| 20 |
+
newNum: '',
|
| 21 |
+
selectedOperation: '',
|
| 22 |
+
previousOperation: '',
|
| 23 |
+
result: ''
|
| 24 |
+
};
|
| 25 |
+
},
|
| 26 |
+
|
| 27 |
+
methods: {
|
| 28 |
+
calculateAndUpdateDisplay() {
|
| 29 |
+
if (this.selectedOperation === '') return;
|
| 30 |
+
if (this.newNum === '') return;
|
| 31 |
+
if (this.previousOperation === '+') {
|
| 32 |
+
this.result = this.display + parseInt(this.newNum);
|
| 33 |
+
} else if (this.previousOperation === '-') {
|
| 34 |
+
this.result = this.display - parseInt(this.newNum);
|
| 35 |
+
} else if (this.previousOperation === '*') {
|
| 36 |
+
this.result = this.display * parseInt(this.newNum);
|
| 37 |
+
} else if (this.previousOperation === '/') {
|
| 38 |
+
this.result = this.display / parseInt(this.newNum);
|
| 39 |
+
}
|
| 40 |
+
this.display = this.result;
|
| 41 |
+
this.newNum = '';
|
| 42 |
+
},
|
| 43 |
+
|
| 44 |
+
clearAll() {
|
| 45 |
+
this.display = 0;
|
| 46 |
+
this.newNum = '';
|
| 47 |
+
this.selectedOperation = '';
|
| 48 |
+
this.previousOperation = '';
|
| 49 |
+
this.result = '';
|
| 50 |
+
},
|
| 51 |
+
|
| 52 |
+
clearNewNum() {
|
| 53 |
+
this.newNum = '';
|
| 54 |
+
},
|
| 55 |
+
|
| 56 |
+
addDecimal() {
|
| 57 |
+
if (this.newNum === '') return;
|
| 58 |
+
this.newNum = `${this.newNum}.`;
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
};
|
| 62 |
+
|
| 63 |
+
// Initialize the calculator component
|
| 64 |
+
const calculator = Daisy.component({
|
| 65 |
+
template: `<div class="flex flex-col">
|
| 66 |
+
<div class="flex flex-wrap">
|
| 67 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '1'">1</button>
|
| 68 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '2'">2</button>
|
| 69 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '3'">3</button>
|
| 70 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '/'">/</button>
|
| 71 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="selectedOperation = '*'">*</button>
|
| 72 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="clearAll()">AC</button>
|
| 73 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="clearNewNum()">CE</button>
|
| 74 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="addDecimal()">.</button>
|
| 75 |
+
<button class="btn btn-lg bg-gray-300 rounded-lg p-2" x-on:click="calculateAndUpdateDisplay()">=</button>
|
| 76 |
+
</div>
|
| 77 |
+
</div>`
|
| 78 |
+
});
|