What is your Index?
What if you wanted to check if a string contains a particular character or substring? One way to do this is using the indexOf(value) function. This function takes a character or string and returns the first index at which it occurs. For example, "the big bang".indexOf("big") returns 4 because "big" starts at index 4. When the character or string provided is not present, this function returns -1. We received an email from an unknown source. The content of the email is stored in the variable message. The way our email application works is if the message contains the word "free", it goes to our spam folder. Find out if the message should go to our spam folder. If the email is spam (e.g. contains "free"), your expression should evaluate to true and otherwise evaluate to false.
message.indexOf("free")>=0 ? true: false
=> false
Nice! That should take care of most of the spam we get.
No comments:
Post a Comment