Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Javascript boolean

Hello expert,

Presently we have the following Javascript 1.1 code

if(email.direct.toLowerCase().indexOf("action.test@lhphospitalgroup.com") != -1){
current.short_description = 'test default short description';
 }
  current.update();



If I remember correctly (== 0) is false. And (== 1) is true.
I think any thing not (== 0) is true
so (== -1) is true
and
 (!= -1) is false.

Is that correct?

If the above statements are correct then code above says:
(where 'email.direct' is an email address)
If the email address, when it has been set to all lower case, is not
"action.test@lhphospitalgroup.com"
then set 'current.short_description'
to be the string 'test default short description'.

Is this correct?

Sorry two questions but they sort of build on each other.

Thanks

Allen in Dallas
0
Allen Pitts
Asked:
Allen Pitts
1 Solution
 
leakim971PluritechnicianCommented:
!!0
is false

!!1
is true

!!10
is true

!!-1
is true

!!-10
is true

What is wrong with :
if(email.direct.toLowerCase().indexOf("action.test@lhphospitalgroup.com") != -1){

Open in new window


is you get same result for this email address too :
ffffffffffffffffffffffffffffaction.test@lhphospitalgroup.comgggggggggggg

You may use instead :
if( /^action\.test@lhphospitalgroup\.com$/i.test( email.direct ) ) {
    // the email address is action.test@lhphospitalgroup.com
}
else {
    // the email address is NOT action.test@lhphospitalgroup.com
}

Open in new window


We use a regular expression

<< ^ >> say it start by
<< $ >> say it end by
<< \ >> is to escape special reserved reg. expr. keyword like the dot << . >>
<< i >> say I don't care about the case
0
 
Tom BeckCommented:
Your description of the logic is correct however indexOf returns the numerical position of the "needle" (test string) in the "haystack" (whole string), not true or false boolean. If the needle is not found, it returns a negative one, -1.
0
 
Allen PittsBusiness analystAuthor Commented:
Great answer. I thought the -1 was a boolean result. Didn't realize it was specific to
indexOf. Thanks.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now