473,809 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best practice if then else

Hi Everyone

I'm wondering what the best practice is for a particular task I'm trying to
accomplish. I'm using two sets of radio buttons for a user to select values
from. These values then go into a couple of tables to show some heat loss
calculations depending on the values chosen I want the tables to have the
relevant values filled in. Is the best way to accomplish this a long list of
if then else type statements or is there a better way of doing this?

http://www.redfedora.co.uk/jscriptradio.html

If anyone could point me in the right direction I'd be really grateful.

Thanks

Bryan
May 21 '06 #1
9 1937
Bryan Hepworth wrote:
Hi Everyone

I'm wondering what the best practice is for a particular task I'm trying to
accomplish. I'm using two sets of radio buttons for a user to select values
from. These values then go into a couple of tables to show some heat loss
calculations depending on the values chosen I want the tables to have the
relevant values filled in. Is the best way to accomplish this a long list of
if then else type statements or is there a better way of doing this?

http://www.redfedora.co.uk/jscriptradio.html

If anyone could point me in the right direction I'd be really grateful.

If there are more than two conditions and these can be expressed as
constant expressions, use a switch statement. It is less error prone
when you expand and easier to read.

Looking at your example, you have an ideal case with the values passed
to your checkxxx functions.

--
Ian Collins.
May 21 '06 #2
If there are more than two conditions and these can be expressed as
constant expressions, use a switch statement. It is less error prone
when you expand and easier to read.

Looking at your example, you have an ideal case with the values passed
to your checkxxx functions.


Tip-Top Ian, Just what I wanted! Now to figure it out and code it up. Thanks
for the swift reply much appreciated.

Bryan
May 21 '06 #3
Bryan Hepworth wrote:
If there are more than two conditions and these can be expressed as
constant expressions, use a switch statement. It is less error prone
when you expand and easier to read.

Looking at your example, you have an ideal case with the values passed
to your checkxxx functions.

Tip-Top Ian, Just what I wanted! Now to figure it out and code it up. Thanks
for the swift reply much appreciated.

No problem, pop back if you have any problems.

--
Ian Collins.
May 21 '06 #4
> No problem, pop back if you have any problems.

--
Ian Collins.


Ian

I'm struggling with the switch format - I couldn't see how I could get it to
check both values and then do the cell changes. I thought I'd illustrate
this with an if elseif, but frustratingly I can't get that to work either!
I've uploaded the changes to the script so far and commented out the if
elseif. http://www.redfedora.co.uk/jscriptradio.html

I'm doing something blindingly obvious wrong but can't suss it out - new to
javascript if you hadn't guessed and this is my first attempt.

Thanks very much.

Bryan
May 21 '06 #5
Bryan Hepworth wrote:
No problem, pop back if you have any problems.

--
Ian Collins.

Ian

I'm struggling with the switch format - I couldn't see how I could get it to
check both values and then do the cell changes. I thought I'd illustrate
this with an if elseif, but frustratingly I can't get that to work either!
I've uploaded the changes to the script so far and commented out the if
elseif. http://www.redfedora.co.uk/jscriptradio.html

I'm doing something blindingly obvious wrong but can't suss it out - new to
javascript if you hadn't guessed and this is my first attempt.

You are trying to do too much in one go. Break the problem down into
bite sized chunks, something like

switch( funacetemp )
{
case 900:

calulateFor900( );
break;

case 1200:

calulateFor900( );
break;

....

default:

//some warning
}

each calulateForN function can use the same form.

--
Ian Collins.
May 21 '06 #6
> switch( funacetemp )
{
case 900:

calulateFor900( );
break;

case 1200:

calulateFor900( );
break;

...

default:

//some warning
}

each calulateForN function can use the same form.


Ian

Whereabouts would this code go - I'm getting totally confused. I was trying
to get it to execute from the calculate button like so:

function docalculation(f urnacetemp)
switch (furnacetemp)
{
case 900:
alert("900");
break;
case 1050:
alert("1050");
break;
case 1150:
alert("1150");
case 1250:
alert("1250");
break;
default
alert("got this far");
}

But I'm getting errors as soon as I load the page.

Thanks

Bryan
May 21 '06 #7
Bryan Hepworth wrote:
switch( funacetemp )
{
case 900:

calulateFor900( );
break;

case 1200:

calulateFor900( );
break;

...

default:

//some warning
}

each calulateForN function can use the same form.

Ian

Whereabouts would this code go - I'm getting totally confused. I was trying
to get it to execute from the calculate button like so:

function docalculation(f urnacetemp)
switch (furnacetemp)
{
case 900:
alert("900");
break;
case 1050:
alert("1050");
break;
case 1150:
alert("1150");
case 1250:
alert("1250");
break;
default

missing :
--
Ian Collins.
May 21 '06 #8
JRS: In article <44************ *********@news. zen.co.uk>, dated Sat, 20
May 2006 22:38:09 remote, seen in news:comp.lang. javascript, Bryan
Hepworth <bryanatredfedo ra.co.uk@?.?> posted :
Hi Everyone

I'm wondering what the best practice is for a particular task I'm trying to
accomplish. I'm using two sets of radio buttons for a user to select values
from. These values then go into a couple of tables to show some heat loss
calculations depending on the values chosen I want the tables to have the
relevant values filled in. Is the best way to accomplish this a long list of
if then else type statements or is there a better way of doing this?


The following scans a set of radiobuttons identified by Rbtn and returns
the value of the element of the array Arr corresponding to the selected
button.

function RadBtns(Rbtn, Arr) { var Q, J=0
while (Q=Rbtn[J]) { if (Q.checked) return Arr[J] ; J++ } }

The array can be supplied as a literal parameter (for a unique set of
buttons) or as a variable. The contents of the array can be of any
type, though it's likely that they will be similar.

From <URL:http://www.merlyn.demo n.co.uk/estr-bcp.htm#33>, in which the
buttons select which Easter function is to be tested (years in error, if
any, would be marked X) :-

function BCPtest(F) { var St, A, B, Y, OK, OKA = ["X", "."], E = 0
with (F) { var Y1 = GetNum(y1), Y2 = GetNum(y2),
BCPn = RadBtns(RBn, [BCPEaster1, BCPEaster2, BCPEaster3]) }
St = FuncName(BCPn) + " : X marks errors\n"
for (Y=Y1 ; Y<=Y2; Y++) { if (Y%10==0) St += "\n"
A = BCPn(Y)
B = EGREaster(Y) ; B = new Date(Date.UTC(Y , B.M-1, B.D))
OK = +A==+B ; if (!OK) E++
St += Y + OKA[+OK] + " " }
document.write( "<pre>", St, "\n Errors ", E, "<\pre>") }

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 21 '06 #9
Lee wrote:
Bryan Hepworth said:
Hi Everyone

I'm wondering what the best practice is for a particular task I'm trying to
accomplish. I'm using two sets of radio buttons for a user to select values
from. These values then go into a couple of tables to show some heat loss
calculation s depending on the values chosen I want the tables to have the
relevant values filled in. Is the best way to accomplish this a long list of
if then else type statements or is there a better way of doing this?

http://www.redfedora.co.uk/jscriptradio.html

If anyone could point me in the right direction I'd be really grateful.

Here's an example using a 3-dimensional array as a lookup table:


Lee

Thanks for posting - I'm really appreciative, must buy you a beer or two!

Bryan
May 22 '06 #10

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

Similar topics

131
21710
by: Peter Foti | last post by:
Simple question... which is better to use for defining font sizes and why? px and em seem to be the leading candidates. I know what the general answer is going to be, but I'm hoping to ultimately get some good real world examples. Fire away! :) Regards, Peter Foti
136
9468
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
4
1844
by: Ned Balzer | last post by:
Hi all, I am pretty new to asp.net; I've done lots of classic asp, but am just beginning to get my mind wrapped around .net. What I'd like to do is include some code that tests if a user is logged in, on each and every page, and redirects the user to a login page if s/he's not logged in. The login page will also take care of some standard setup, such as choosing/populating a user profile. I used to use <!-- #include ... --for this,...
16
2824
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. Two Questions: 1) BUILT-IN to Visual Studio 2005. What ideas do you have to quickly
17
3050
by: 2005 | last post by:
Hi In C++, are the following considered best practices or not? - passing aguments to functions (ie functions do not take any arguments ) - returning values using return statement Anything else? The reason for this question is that I had an assignment in which I was
4
2899
by: =?Utf-8?B?bW9mbGFoZXJ0eQ==?= | last post by:
In VB6, we created a number of ActiveX DLLs that all shared a similar interface. The main application would load these in dynamically (late-bound.) This worked well for our situation because we could update the DLLs independently of the main EXE, and performance was not a problem. We want to do the same thing in C# (at least test it), but we are not exactly clear what the best practice is. We would prefer to do something similar, but we...
13
2574
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before submit, which determines exactly where to send the form contents. The table of "receipients" could contain in the region of 3,500 recipients but is more likely to contain up to 1,000. Table structure:
56
5213
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be checked on each iteration, since the Count changes as you delete elements. I would think it is guaranteed to be computed each time, and not cached. So, is this the best way?
52
3399
by: burgermeister01 | last post by:
First, let me say that this question is a rather general programming question, but the context is PHP, so I figured this group would have the most relevant insight. Anyways, this is also more of an opinion based question than one seeking a definite answer. Recently, while maintaining a rather large system. I needed to add an array class member to an object. It was exactly the same as another class member, except that one array stored...
0
9721
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
9601
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
10376
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
10379
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6881
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
5550
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
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.