Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Indexof() issue

Derek10
Tera Expert

I'm having a quandary trying to see why this is not working, I'd like to think it is a fairly simple thing and I have been looking at screens too long this week!

answer = ifScript();
//
var location = current.variables.requested_for.location.getDisplayValue();

(sample value of location is UKBAS1 -blah blah blah)

function ifScript() {
if (location.toString().indexOf('UKBAS1') >= 0) {
  return 'yes';
 
}
return 'no';
}

I've tried == 0, != -1 as well. Just looking to test for a match if the string contains UKBAS1

Has to be something simple here

1 ACCEPTED SOLUTION

brianlan25
Kilo Patron

Hello Derek,


I am able to do thing like this.   The only difference between my code and yours is where I declare my variables.   I declair them in the fucation not before it.


answer = ifScript();
//


function ifScript() {


var location = current.variables.requested_for.location.getDisplayValue();
if (location.toString().indexOf('UKBAS1') >= 0) {
  return 'yes';
 
}
return 'no';
}


View solution in original post

6 REPLIES 6

srinivasthelu
Tera Guru

Hi Derek,



That code looks ok. I would add couple of logs and check logs.



var location = current.variables.requested_for.location.getDisplayValue();


gs.log("location"+location);



function ifScript() {


gs.log("location.toString().indexOf('UKBAS1') "+location.toString().indexOf('UKBAS1') );
if (location.toString().indexOf('UKBAS1') >= 0) {
  return 'yes';
 
}
return 'no';
}



Thanks


Srini


sabell2012
Mega Sage

Derek:



So I tried your code out in Scripts - Background (Fix Scripts if you are in Geneva), and found a couple of possible issues.   Here is the code:



// Prep work for test


var current = {};


current.variables = {};


current.variables.requested_for = {};


current.variables.requested_for.location = 'uKBas1';   // you never know what your data will actually look like




//var location = current.variables.requested_for.location.getDisplayValue();


location = current.variables.requested_for.location;


gs.print("location:" + location);



gs.print(ifScript());



function ifScript() {


    gs.log("location.toString().toUpperCase().indexOf('UKBAS1'):" + location.toString().toUpperCase().indexOf('UKBAS1'));



  // Good practice to upper/lowercase before the test.   Good practice to test on > -1 for indexOf


    if (location.toString().toUpperCase().indexOf('UKBAS1') > -1) {


          return 'yes';


    }



    return 'no';


}



This code finds the string.   Bet there is a case issue in there.   🙂



Steven.


dhoffman
Tera Contributor

I'm a HYUUGE (said in my best Donald Trump voice) fan of Regular Expressions instead of indexOf() for string parsing and manipulation.   I suppose I never got the hang of the "-1" or "0" result and prefer to get a "true/false" result instead.   If there's a lot of strings or patterns to test, it can save lines of code.



Consider:



if (/UKBAS1/ig.test(location.toString()) {


        return 'yes';


}



  • The regular expression is between the two slashes, so /UKBAS1/ig and the "i" means "ignore case" and the "g" means "global".
  • The test() method returns true/false and takes a string input, in this case location.toString() is passed into the test.


Just another option to play with.



Great references:


brianlan25
Kilo Patron

Hello Derek,


I am able to do thing like this.   The only difference between my code and yours is where I declare my variables.   I declair them in the fucation not before it.


answer = ifScript();
//


function ifScript() {


var location = current.variables.requested_for.location.getDisplayValue();
if (location.toString().indexOf('UKBAS1') >= 0) {
  return 'yes';
 
}
return 'no';
}