Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > nesting functions

Reply
Thread Tools

nesting functions

 
 
Philip WATTS
Guest
Posts: n/a
 
      10-31-2003
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


 
Reply With Quote
 
 
 
 
Douglas Crockford
Guest
Posts: n/a
 
      10-31-2003
> 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

 
Reply With Quote
 
 
 
 
e
Guest
Posts: n/a
 
      10-31-2003
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 ,
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" <> wrote in message
news:bnug4o$ts6$...
> 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
>
>



 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-31-2003
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
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      10-31-2003
Thomas 'PointedEars' Lahn <> 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 -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-01-2003
Lasse Reichstein Nielsen wrote:

> Thomas 'PointedEars' Lahn <> 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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-01-2003
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)
 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      11-04-2003
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 <>

* 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


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-23-2003
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
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      11-23-2003
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nesting variadic functions Boltar C Programming 4 09-17-2007 10:43 PM
nesting of member functions newbai C++ 3 01-13-2007 12:34 PM
Module.nesting -> Kernel#nesting Trans Ruby 10 09-16-2005 12:21 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
group nesting David K MCSE 6 12-12-2003 04:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57