Categories
Vue 3 Projects

Create a Palindrome Checker with Vue 3 and JavaScript

Spread the love

Vue 3 is the latest version of the easy to use Vue JavaScript framework that lets us create front end apps.

In this article, we’ll look at how to create a palindrome checker with Vue 3 and JavaScript.

Create the Project

We can create the Vue project with Vue CLI.

To install it, we run:

npm install -g @vue/cli

with NPM or:

yarn global add @vue/cli

with Yarn.

Then we run:

vue create palindrome-checker

and select all the default options to create the project.

Create the Palindrome Checker

To create the palindrome checker, we write:

<template>
  <form>
    <div>
      <label>word to check</label>
      <input v-model="word" />
    </div>
  </form>
  <div>Is Palindrome:{{ isPalindrome ? "Yes" : "No" }}</div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      word: "",
    };
  },
  computed: {
    isPalindrome() {
      const { word } = this;
      return word === word.split("").reverse().join("");
    },
  },
};
</script>

We have the input field in the form.

And we bind the inputted value to the word reactive property with v-model .

Then below that, we have the data method that returns the word reactive property.

Then we add the isPalindrome computed property to check if this.word is a palindrome.

We check if it’s a palindrome by checking if the reversed string is the same as the original.

We call split to split it into an array of characters.

Then we call reverse to reverse the array.

And then we call join to join the characters back into a string.

Then we display that value in the template.

Conclusion

We can create a palindrome checker easily with Vue 3 and JavaScript.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *