Solved

Javascript boolean

Posted on 2014-01-29
3
310 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
ID: 39817948
!!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
ID: 39817954
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
ID: 39818200
Great answer. I thought the -1 was a boolean result. Didn't realize it was specific to
indexOf. Thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
adding text to textboxes based on dropbox choice 13 45
age from date of birth 4 43
Jquery GMAP3 Hierarchy 1 28
How can I enable / disable a text box with a checkbox 3 19
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

821 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