Solved

Javascript boolean

Posted on 2014-01-29
3
312 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Asp in script 6 36
I thought I had multiple sliders on one page working properly . . . I don't. 10 16
100% tall div not scrollable on iPhone 3 20
Insert Button on a table 16 36
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. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

791 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