Skip to content

Commit

Permalink
calculator-app
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmenshikov committed Aug 18, 2024
1 parent d7a9348 commit 2920da1
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions 02-basics-2/30-calculator/CalculatorApp.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
import { defineComponent } from 'vue'
import { defineComponent, ref, watch } from 'vue'

export default defineComponent({
name: 'CalculatorApp',

setup() {},
setup() {
const firstNumber = ref(0);
const secondNumber = ref(0);
const picked = ref('sum');
const result = ref(0);

watch([firstNumber, secondNumber, picked], () => {
switch(picked.value) {
case 'sum':
return result.value = firstNumber.value + secondNumber.value
case 'subtract':
return result.value = firstNumber.value - secondNumber.value
case 'multiply':
return result.value = firstNumber.value * secondNumber.value
case 'divide':
return result.value = firstNumber.value / secondNumber.value
}
});

return {
firstNumber,
secondNumber,
picked,
result,
}
},

template: `
<div class="calculator">
<input type="number" aria-label="First operand" />
<input type="number" aria-label="First operand" v-model="firstNumber" />
<div class="calculator__operators">
<label><input type="radio" name="operator" value="sum"/>➕</label>
<label><input type="radio" name="operator" value="subtract"/>➖</label>
<label><input type="radio" name="operator" value="multiply"/>✖</label>
<label><input type="radio" name="operator" value="divide"/>➗</label>
<label><input type="radio" name="operator" value="sum" v-model="picked" />➕</label>
<label><input type="radio" name="operator" value="subtract" v-model="picked" />➖</label>
<label><input type="radio" name="operator" value="multiply" v-model="picked" />✖</label>
<label><input type="radio" name="operator" value="divide" v-model="picked" />➗</label>
</div>
<input type="number" aria-label="Second operand" />
<input type="number" aria-label="Second operand" v-model="secondNumber" />
<div>=</div>
<output>0</output>
<output>{{ result }}</output>
</div>
`,
})

0 comments on commit 2920da1

Please sign in to comment.