Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to check given string is Palindrome or not using filter in...

How to check given string is Palindrome or not using filter in VueJS ?

Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js template. To check string is palindrome or not using filters, we have to first reverse the string and accordingly write the logic of string palindrome and apply the filter on the string. Both the example is working on different approach.

Example 1:

index.html




<!DOCTYPE html>
<html lang="en">
<head>
    <script 
        src=
    </script>
</head>
<body>
    <div id='parent'>
          
        <p>
          <strong>String1 : </strong>
          {{ st1 | pCheck }}
        </p>
  
          
        <p>
          <strong>String2 : </strong>
          {{ st2 | pCheck }}
        </p>
    </div>
    <script src='app.js'></script>
</body>
</html>


app.js




const parent = new Vue({
    el : '#parent',
    data : {
        st1 : 'I am not',
        st2 : 'iTopiNonAvevAnoNipoTi',
    },
  
    filters:{
        pCheck : function(data){
            var rev = [];
            for (let i = data.length - 1, j = 0; i >= 0; i--, j++)
                rev[j] = data[i];
            reverse = rev.join('');
            if(data == reverse) return 'Palindrome'
            else return 'Not Palindrome'
        }
    }
})


Output:

Example 2:

index.html




<!DOCTYPE html>
<html lang="en">
<head>
    <script 
       src=
    </script>
</head>
<body>
    <div id='parent'>
        <p>
          <strong>String1 : </strong>
          {{ st1 | pCheck }}
        </p>
        <p>
          <strong>String2 : </strong>
          {{ st2 | pCheck }}
        </p>
  
    </div>
    <script src='app.js'></script>
</body>
</html>


app.js




const parent = new Vue({
    el : '#parent',
    data : {
        st1 : 'I am not',
        st2 : 'iTopiNonAvevAnoNipoTi',
    },
  
    filters:{
        pCheck : function(data){
            const reverse = data.split("").reverse().join("");
            if(data == reverse) return 'Palindrome'
            else return 'Not Palindrome'
        }
    }
})


Output:

palindrome check using filters

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments