How to compare the value of variables with a String?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2018 01:16 AM
Hello,
I hav e written a code which should return 'yes" if the value of a variable is equal to a particular string.I have mentioned the code below:-
answer = ifScript();
var oper = "MODIFY_ENTITY";
function ifScript(){
if(oper == "MODIFY_ENTITY"){
return 'yes';
}
else{
return 'no';
}
}
gs.print(answer);
It should return answer to be yes but here it is returning the answer to be no.Why is it behaving like that?
Is there anything wrong with this code,If yes then please tell me the correct approah and what I am doing wrong here.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2018 01:20 AM
Hello,
you assigned the values a bit wrong. Just adjust your code slightly to assign the variable value beforte the function call and hand it over to the function.
// first assign the value, then call the function
var oper = "MODIFY_ENTITY";
answer = ifScript(oper);
// the function needs a variable "operator"
function ifScript(operator){
if(operator== "MODIFY_ENTITY"){
return 'yes';
}
// you can cut else here, because you have a return above which exits the function
return 'no';
}
gs.print(answer);
Hope this helps.
Greetings