473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

if; else if logic

110 100+
I was under the impression that if you have a series of "if, else if, else if " clauses presented mutually exclusive alternatives. Apparently that is not the case. This code results in the var "issue" being copied into all three objects. How can I write it so the results are mutally exclusive?

Expand|Select|Wrap|Line Numbers
  1. var issue = "Property has been vacant more than 24 months";
  2.                 if(document.getElementById('issue1').value==""){
  3.                     document.getElementById('issue1').value = issue;
  4.                 } else if(document.getElementById('issue2').value==""){
  5.                     document.getElementById('issue2').value = issue;
  6.                 } else if(document.getElementById('issue3').value==""){
  7.                     document.getElementById('issue3').value = issue;
  8.                 }
  9.  
Mar 4 '09 #1
4 1322
gits
5,390 Expert Mod 4TB
unless there is a loop or whatever around this code it is exclusive ... you might try the following similar example:

Expand|Select|Wrap|Line Numbers
  1. var foo = 'bar';
  2.  
  3. var n1 = '';
  4. var n2 = '';
  5. var n3 = '';
  6.  
  7. if (n1 == '') {
  8.     n1 = foo;
  9. } else if (n2 == '') {
  10.     n2 = foo;
  11. } else if (n3 == '') {
  12.     n3 = foo;
  13. }
  14.  
  15. alert(n1);
  16. alert(n2);
  17. alert(n3);
btw. it would be better to use a switch(); statement for such series of decisions ...

kind regards
Mar 4 '09 #2
acoder
16,027 Expert Mod 8TB
The code should work. Show the context in which it's used: the HTML code, the function and where/how it's called.
Mar 4 '09 #3
andersond
110 100+
I would like the switch() suggestion; but, every example I found had the case resolve to a number. I don't think that will work.

Here's what I'm trying to do. I have 31 questions a retail insurance agent must answer to get a quotation on an insurance policy submission. Any of the 31 can result in the submission being referred to underwriting, rather than being quoted. In order to speed up the process of determining what caused it to be referred I want to record the first 10, or fewer, problems (issue1 ...issue10). If the first problem occurs on question 27 I want the result to go into "issue1". And, if there are problems with #3, #14 and #30 I want those responses to go into issue1, issue2 and issue3. I want it to find the first empty field and fill it with the appropriate resonse -- and leave all the others blank. Hence my logic was to test if issue1="" and, if it does, fill it with the response for that question -- and so on until it found a blank field.
Mar 4 '09 #4
gits
5,390 Expert Mod 4TB
in case you have a list and just want to fill the first empty, then a loop would be the best solution i think ... something like:

Expand|Select|Wrap|Line Numbers
  1. // ordered list of field ids
  2. var fieldIds = {
  3.     issue1: 1, issue2: 1, issue3: 1
  4. };
  5.  
  6. for (var i in fieldIds) {
  7.     var field = document.getElementById(i);
  8.  
  9.     if (field.value == '') {
  10.         field.value = issue;
  11.         break;
  12.     }
  13. }
kind regards
Mar 4 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
12
by: Roman Töngi | last post by:
In c++ there does not exist an if-statement as for example in Visual Basic, does it? //VB-analogy: if (cond.) statement; else if (cond.) statement; else statement;
14
by: Ørjan Langbakk | last post by:
I have a form where the user has the possibility to enclose his name. email, address and phonenumber. I want to be able to check if some of the fields are filled - at least one. This is so that...
19
by: tobiah | last post by:
def foo(thing): if thing: return thing + 1 else: return -1 def foo(thing): if thing:
3
by: divya | last post by:
Hiii, Please read the following piece of code:- function SendToWebServer1(sendto) //sendto string contains a URL { if (document.form1.txtbookedby.value == "") { alert('your name field...
6
by: Logan Lee | last post by:
Hi. What is the difference between if {} if {} if {} if else {} ?
23
by: bearophileHUGS | last post by:
So far in Python I've almost hated the 'else' of the 'for' loops: - I have problems to remember its meaning; - It gives me little problems when I later want to translate Python code to other...
11
by: Chad | last post by:
The question stems from some code at the following url http://www.cplusplus.com/reference/clibrary/cstdio/fread.html In the code example they have a single if statement for the following ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.