Solved

Javascript boolean

Posted on 2014-01-29
3
305 Views
Last Modified: 2014-01-29
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
Comment
Question by:9apit
3 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
!!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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:9apit
Comment Utility
Great answer. I thought the -1 was a boolean result. Didn't realize it was specific to
indexOf. Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now