Solved

Javascript boolean

Posted on 2014-01-29
3
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

622 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