473,666 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple code not working

Why doesn't this work?:

var backlink = readCookie("sty le");

if (backlink == ('medium' || 'large' || 'huge')) {

document.write( " ... ");

}

It works if 'backlink' equals medium, but doesn't work on the others.
What am I missing?

-- Richard


Jul 20 '05 #1
1 1352
"Richard Barnet" <rb*****@csaNOS PAMreno.org> writes:
"Richard Barnet" <rb*****@csaNOS PAMreno.org> wrote in message
news:bg******** **@nnrp.atgi.ne t...
Why doesn't this work?:

var backlink = readCookie("sty le");
if (backlink == ('medium' || 'large' || 'huge')) {
document.write( " ... ");
}

It works if 'backlink' equals medium, but doesn't work on the others.
What am I missing?

The expression
('medium' || 'large' || 'huge')
uses short-circuit "or" operators.

Let's ignore the last "|| 'huge'" part for now, and just look at
"'medium' || 'large'".

An "or"-expression is true if either operand is true. The short-circuit
implementation of an "or"-expression checks the first operand first, and
if that is true, it never evaluates the second.

Since the first operand ('medium') is true when converted to a
boolean, the entire "or" expression is true, and it even evalutates to
'medium'.

Add "|| 'huge'", and it still evaluates to 'medium'.

That is why the comparison is true if "backlink" is the string "medium",
because it is compares to an expression that evaluates to "medium".
If I lose the second set of quotes:
Parentheses, not quotes. Quotes would be ' and ".
if (backlink == 'medium' || 'large' || 'huge') { document.write( " ...
"); }

then it executes the code no matter what backlink equals (i.e., if
backlink == 'small', it still writes the code).


Because you now have (if I add parentheses to show grouping):
(backlink == 'medium') || 'large' ...
If "backlink" is "medium", this gives true (or'ed with something else,
which is still true).
If "backlink" is not "medium", it gives false or'ed with 'large',
which gives 'large', which is also true when converted to a boolean
(as any non-empty string).

You need to compare to each possibility in turn:

if (backlink == 'medium' || backlink == 'large' || backlink == 'huge')...

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1294
by: Chris Leonard | last post by:
I am a C# newbie with experience in other languages who is writing a C# app that talks to a SQL Server. I have the connection working, the queries are working, error handling (try/catch/finally) is working, and life is (mostly) good. However, I tried to use a delegate to display informational and warning message (these are non-error messages not trapped by catch) and this is *not* working. Here is what I believe to be the relevant part...
17
6512
by: savesdeday | last post by:
In my beginnning computer science class we were asked to translate a simple interest problem. We are expected to write an algorithm that gets values for the starting account balance B, annual interest rate I, and annual service charge S. Your algorithm would then compute and print out the total amount of interest earned during the year and the final account balance at the end of the year (assuming that interest is compounded monthly, and...
5
2631
by: calaha | last post by:
Hi all, I have been working with this since last night, and can't quite figure out why it's not working. I have a simple login box form that is set to be my startup form in my Access app (upon successful authentication, it opens the Switchboard). All it does is get the username and password, pull up a recordeset of the usernames and sets the UserId to be used throughout the session. The table Userlist is a SQL 2000 linked table. I can...
2
8387
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when submitting the form to update the database. The server doesn't have the client side value any more. It seems to me that as I begin to write the client side javacript code for form validation and client side editing capabilities in order to save...
5
1797
by: Logickle | last post by:
Hi, all. I'm working on an application which requires communicating session info between separate web apps running on the same web server. The out of process server method sounded ideal, and very simple to implement as far as I could tell. So I re-configured my apps' web.config and machine.config files from InProc to StateServer, and confirmed that's the mode used by both web apps. To keep it simple, I created two basic ASP.NET apps...
6
9401
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some simple one. Exercise 4-2: Write a program to print a block E using asterisks (*), where the E has a height of seven characters
24
6321
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to have on his site, a Javascript Calculator for working out the cost of what they want, for example: 1 widget and 2 widglets = £5.00
10
110212
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and it also integrates UPX (Ultimate Packer for eXecutables) into the build process, if you have it installed. It also claims functionality on linux, as a bonus (I didn't test this). I highly recommend the UPX options, and will cover how to get it...
2
1671
by: Bhasker V Kode | last post by:
Hi, I just wanted to share with everyone a new project that i'm launching today called the Returnable Project ( http://returnable.org ) Returnable serves as a open-platform architectural guide to advance the use of the URL as a DSL for specifying 3 traits : content,its delivery ,and its behaviour on the web through the use of Returnable elements embedded within web pages .
2
1335
by: kigoobe | last post by:
I am having a very weird issue here, a simple form submit not working. Trying for several hours now ... must be something stupid ... help appreciated. First, the html - <form id="formFinale" method="post" action="noscript.php" enctype="application/x-www-form-urlencoded"> <div class="myStyle">Votre nom ? <input type="text" size="36" name="ouchocolat" id="ouchocolat" value="" /></div> <div class="myStyle">J’ai lu et je suis...
0
8445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.