473,396 Members | 1,760 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,396 software developers and data experts.

nesting functions

I am trying to carry out multiple checks on some input data. I am doing this
by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?

Many thanks
Phil
Jul 20 '05 #1
13 2215
e
Have each function return a boolean indicating it's success. Sorry js isn't
my forte I'm just lurnking while I wait for an answer to one of my posts :p,
so syntax could be way off. But general idea is this:

function checkTheInput()
{
if (firstCheck())
{
if (secondCheck())
{
if (thirdCheck())
{
alert('all 3 cheks were ok');
}
else
{
alert('thirdCheck() failed');
}
}
else
{
alert('secondCheck() failed');
}
{
else
{
alert('firstCheck() failed');
}
}

function firstCheck()
{
//if data is ok return true, otherwise return false
}

function secondCheck()
{
//ditto
}

etc...

"Philip WATTS" <PR*****@syringa.freeserve.co.uk> wrote in message
news:bn**********@news8.svr.pol.co.uk...
I am trying to carry out multiple checks on some input data. I am doing this by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the script.

How do I overcome this problem?

Many thanks
Phil

Jul 20 '05 #2
> I am trying to carry out multiple checks on some input data. I am doing this
by the running the data through a number of functions. i.e. I have an
onclick that calls a function, which in turn calls the test functions.

My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?


Have each function return true if the input is ok. Then use if statements.
if (test1()) {
if (test2()) {
if (test3()) {
...

http://www.crockford.com/#javascript

Jul 20 '05 #3
Philip WATTS wrote:
My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?


Instead of using nested if-statements you could simply `return false'
if a check fails. I find this more practical since you can add tests
without further nested block statements (and without indentation which
should have been done then for the sake of legibility):

function testMe()
{
if (!test1())
return false;
if (!test2())
return false;
if (!test3())
return false;

return true; // passed all tests
}
PointedEars
Jul 20 '05 #4
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
function testMe()
{
if (!test1())
return false;
if (!test2())
return false;
if (!test3())
return false;

return true; // passed all tests
}


That sounds like a job for short-circuit boolean operators!

function testMe() {
return test1() && test2() && test3();
}

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5
Lasse Reichstein Nielsen wrote:
Thomas 'PointedEars' Lahn <Po*********@web.de> writes:
function testMe()
{
if (!test1())
return false;
if (!test2())
return false;
if (!test3())
return false;

return true; // passed all tests
}


That sounds like a job for short-circuit boolean operators!

function testMe() {
return test1() && test2() && test3();
}


Not if you, like the OP, want to know *which*
test failed (which I omitted in the above code):

function ...(...)
{
if (!test1())
alert("Test 1 failed!");
return false;
}
...
return true; // passed all tests
}
PointedEars
Jul 20 '05 #6
Thomas 'PointedEars' Lahn wrote on 01 nov 2003 in comp.lang.javascript:
Not if you, like the OP, want to know *which*
test failed (which I omitted in the above code):

function ...(...)
{
if (!test1())
alert("Test 1 failed!");
return false;
}
...
return true; // passed all tests
}


Thet you should put an extra { where it belongs:

function testing() {
if !test1() {
alert("Test 1 failed!");
return false;
}
...
alert("passed all tests");
return true;
}

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #7
Thomas 'PointedEars' Lahn wrote:
Philip WATTS wrote:
My problem is getting the testing to stop if one of the tests fails and
await for the input to be amended.
I believe that this is because the when the testing function has finished it
returns the script to the point immediately AFTER the function was called,
i.e. it simply carries on to the next test and hence to completetion of the
script.

How do I overcome this problem?


Instead of using nested if-statements you could simply `return false'
if a check fails. I find this more practical since you can add tests
without further nested block statements (and without indentation which
should have been done then for the sake of legibility):

function testMe()
{
if (!test1())
return false;
if (!test2())
return false;
if (!test3())
return false;

return true; // passed all tests
}

PointedEars


This is known as a "gauntlet" <url: http://mindprod.com/jgloss/gauntlet.html />,
specfically an "Early Return Style Gauntlet".

I used to detest this type of code, it seemed sloppy and ugly, however, as I write
more and more code, I find myself using the style more and more often, because as
Roedy points out "I like this style because the conditions are independent and
uniform. You can shuffle the order easily and add new conditions without having
the adjust the existing code.".

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #8
Evertjan. wrote:
Thomas 'PointedEars' Lahn wrote [...]:
Not if you, like the OP, want to know *which*
test failed (which I omitted in the above code):

function ...(...)
{
if (!test1())
alert("Test 1 failed!");
return false;
}
...
return true; // passed all tests
}


Thet you should put an extra { where it belongs:

function testing() {
if !test1() {


Also nitpicking, the `if' statement requires
parantheses around the conditional expression.
PointedEars
Jul 20 '05 #9
Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:
Thet you should put an extra { where it belongs:

function testing() {
if !test1() {


Also nitpicking, the `if' statement requires
parantheses around the conditional expression.


Pick nit and be my guest ;-)

"requires" by definition or by erroring out ?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #10
"Evertjan." <ex**************@interxnl.net> writes:
Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:
Also nitpicking, the `if' statement requires
parantheses around the conditional expression.

"requires" by definition or by erroring out ?


Requres by the syntax rules of Java/ECMAScript. Without them, it
is not an if statement, just a syntax error.

(I don't know what "erroring out" means).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #11
Lasse Reichstein Nielsen wrote on 23 nov 2003 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.net> writes:
Thomas 'PointedEars' Lahn wrote on 23 nov 2003 in comp.lang.javascript:
Also nitpicking, the `if' statement requires
parantheses around the conditional expression.
"requires" by definition or by erroring out ?


Requres by the syntax rules of Java/ECMAScript. Without them, it
is not an if statement, just a syntax error.


Since when is a statement not a statement if the subsequent syntax is not
according to the rule book? In your definition, which is not mine, a
statement can never be syntactically incorrect, it seems.
(I don't know what "erroring out" means).


Does it give an error or does it work as intended [by me and anyone
reasonable]? There is a big difference between syntactical correctness and
a working script, in both possible scenarios: Some syntactical incorrect
scripting works, some syntactical correct scripting doesn't and then you
get an error, a crash or an unforseen and inintended result. In those two
circumstances it the working incorrect script seems preferable.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #12
"Evertjan." <ex**************@interxnl.net> writes:
Since when is a statement not a statement if the subsequent syntax is not
according to the rule book?
What do you mean by "subsequent"?

The syntax of an "if" statement is:
keyword "id"
left parenthesis, "("
expression
right parenthesis, ")"
statement
(optional: keyword "else" + statement)

Omitting the parentheses makes it no more an "if" statement that
omitting "function" makes something a function declaration.
In your definition, which is not mine, a
statement can never be syntactically incorrect, it seems.
Exactly.
In fact, if the syntax is incorrect, not only isn't it a statment,
it's not even Javascript (or maybe more correct, not even ECMAScript,
since Javascript doesn't have as exact a definition).

A syntax error is just that: an error. It might have been intended
to be a statement, but it isn't.
(I don't know what "erroring out" means).


Does it give an error or does it work as intended [by me and anyone
reasonable]?


It gives an error. It fails to compile correctly, which also means
that the entire file/script element it is in, is ignored.
There is a big difference between syntactical correctness and
a working script, in both possible scenarios: Some syntactical incorrect
scripting works,
Correct. Some interpreters allow things not in the official syntax,
like function declarations inside block statements or <!-- for comments.

This is not one of those. You can no more omit the parenteses of an
if expression than you can those of a while, switch or for expression.
some syntactical correct scripting doesn't and then you
get an error, a crash or an unforseen and inintended result.
That would be a runtime error, e.g., accessing a property of the null
value.
In those two circumstances it the working incorrect script seems
preferable.


Not working at all is preferable to working incorrectly. :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #13
Lasse Reichstein Nielsen wrote on 23 nov 2003 in comp.lang.javascript:
In those two circumstances it the working incorrect script seems
preferable.


Not working at all is preferable to working incorrectly. :)


You misread my point.

No, no, an incorrect (specs wise) script could work as intended.

And a correct (specs wise) script could work incorrectly.

An example comes to mind: the Jscript toFixed().
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #14

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

Similar topics

0
by: Wolfgang Schwanke | last post by:
Dear usenet, I'm having the following small problem. I've been ask to add some Quicktime panoramas to a website. The author of the panoramas has made two versions of each: One in MOV format,...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
18
by: José de Paula | last post by:
Does C99 support defining a function in the body of another function? I mean, something like: int a (void) { int x; int y; int b(int c) {
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
4
by: kl.vanw | last post by:
I would like to count the nesting level in template classes. How can I make the following work? #include <assert.h> template <class T> class A { public: A() { // what goes here?
3
by: newbai | last post by:
hi!! need some help with turbo c++ I am trying to nest member functions.what I did was try to access a member function with is public thro another function which is public,but turbo c++ gives a...
6
by: stephen.cunliffe | last post by:
Hi, I'm looking for opinion/facts/arguments on the correct nesting of UL, OL, & LI elements. For example, this is what I want (unordered list): * Item 1 * Item 2 * Item 3
4
by: Boltar | last post by:
Hi Is it possible to nest variadic functions? Eg to do something like this: void mainfunc(char *fmt, ...) { va_list args; va_start(args,fmt);
17
by: henry | last post by:
Folks Here's a skeleton, generic HTML page, call it "index.php". You'll see a bit of php code in the middle: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.