0

I have this function:

function validateVip(){ 
    $("table[id=table-pss] tbody tr").each(function() {
        var keval = $(this.cells[4]).find('input');
        var data = $(keval[0]).val();
        console.log("result: " + data);
        console.log(data.includes([vip]));
        var x;                      
        if ((data.includes([vip])) == true)
        {
            x = "yay";
            console.log("Vip Exists");                          
        }
        else
        {
            x = "nay";
            console.log("No VIP");
        }                       
        return x;
  });
}

Whenever I call it, it always return "undefined". Here is how I call the function validateVip:

var isVip = validateVip();
            console.log("is vip:" + isVip);

Can someone show me what I did wrong?

Thank you.

7
  • 1
    At least you forgot return statement in your validateVip() function Commented Aug 8, 2020 at 2:09
  • Why are you putting vip in an array when you do data.includes([vip])? Commented Aug 8, 2020 at 2:15
  • @Barmar because I have a list of VIP in array and need to campare it whether html table has vip list in it. I just need a variable to notify if vip exists or not. Commented Aug 8, 2020 at 2:18
  • 1
    If vip is already an array, and you want to know if data is in it, it should be vip.includes(data) Commented Aug 8, 2020 at 2:24
  • 1
    data is a string, it can't include an array. Commented Aug 8, 2020 at 2:25

1 Answer 1

1

The value of x is returned by the callback passed to $().each, which does nothing

You need to define the variable x in the upper scope:

function validateVip() {
    var x;
    $("table[id=table-pss] tbody tr").each(function() {
        var keval = $(this.cells[4]).find('input');
        var data = $(keval[0]).val();
        console.log("result: " + data);
        console.log(data.includes([vip]));

        if ((data.includes([vip])) == true) {
            x = "yay";
            console.log("Vip Exists");
        } else {
            x = "nay";
            console.log("No VIP");
        }
    });
    return x;
}

Note that x will be equaled to the last item assignment, iterated using each

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.