Computer scienceFrontendVue.jsComputable Properties

Watching for changes

Age categorization

Report a typo

You're building an age categorization app and want to display the category based on the user's input age. The below Vue application uses an input field for age. Use a watcher to display an age category based on the input value (e.g., "Child," "Teen," "Adult").

Fill in the gaps with the relevant elements
 <template>
  <div>
    <label>Enter your age:</label>
    <input v-model="age" type="number" />
    <p>Your age category is: {{ ageCategory }}</p>
  </div>
</template>

<script setup>
import { ref, watch } from 'vue'

const age = ref(0)
let ageCategory = ''

(, () => {
  if (newAge < 18) {
    ageCategory = 'Child'
  } else if (newAge < 40) {
    ageCategory = 'Teen'
  } else {
    ageCategory = 'Adult'
  }
})
</script>
ChildageCategoryTeenAdultnewAgeagenewagewatch

Create a free account to access the full topic