How to compare the value of variables with a String?

ishaanvohra
Kilo Contributor

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.

1 REPLY 1

Fabian Kunzke
Kilo Sage
Kilo Sage

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