I have the following snippet of code :
return (AllowableCharacters.indexOf(String.fromCharCode(k)) != -1);
Now, I don't quite get the usage of -1 in this script.
The way the script reads is as follows:
first String.fromCharCode(k) != -1
is executed (k is a key code , I am dynamically getting from some other script).
Then I get the indexof(String.fromCharCode(k) != -1) from AllowableCharacters.
Which is a string something like this:
AllowableCharacters = "abc" ;
I also understand that if the indexof can't find a value in the above string it return -1.
Coming back to my question, however, why the -1 in the script?
EDIT ::
To make my question simpler , how would you read the following line :
String.fromCharCode(k))!=-1
in simple plain english .
EDIT 2::
ok so i just read guffa's answer and made a random script to check , heres the script :
var store = "abcdefgpS";
function check_index(){
console.log(store.indexOf(String.fromCharCode(83)));
};
in the above function the !-1 is excluded , so on console.log , if a match is found we get the indexOf where the character was found and well if a match is not found we get -1 .
NOW , now thats not what we want , what we want is a "tell me if the value is there"(return true) or "tell me if the value is not there"(return false).
so what we do is we change the above script to :
var store = "abcdefgpS";
function check_index(){
console.log(store.indexOf(String.fromCharCode(83)) !-1);
};
which, gives ur true or false values .
now how does :
return (store.indexOf(String.fromCharCode(83)) !-1)
read as :
if (store.indexOf(String.fromCharCode(83)) !-1){
return true;
}else { return false; }
I don't see the if statement in .
return (store.indexOf(String.fromCharCode(83)) !-1);
Thank you,
Alexander