Sorry to bother with my newbie-ness :P

J

Jai

but can someone tell me why this function isnt working?

<p align="center">
<h3>Do you wish to confirm your booking? Just enter your code
here!</h3>
</p>
<p align="center">
<input type="text" name="bookingno" id="bookingno" size="6"
maxlength="6">
</p>
<p align="center">
<input type="submit" name="confirm" value="Confirm This Booking"
onclick="validate_booking()">
</p>


<script type="text/javascript" language="JavaScript">
<!--hide from no-script browsers -->

var j = 1

function validate_booking() {
while (bookingno != Booking[j] && j != 7) {
j++
}
if (bookingno = Booking[j]) {
window.alert("yes");
} else {
window.alert("no");
}
}
</script>
 
O

orlando echevarria

Several things going on here:

1 - this statement:

var j = 1

should be:

var j = 1;

2 - this statement:

j++

should be:

j++;

3 - this statement:

if (bookingno = Booking[j]) { // incorrect since you are making an
assignment and not a comparision

should be:

if (bookingno == Booking[j]) {

4 - You need to declare a form or at least I would:

<form name = "newbieFrm" method = "post" action = "http://myURL">
<input type="text" name="bookingno" id="bookingno" size="6"
maxlength="6">
.....
.....
</form>

5 - how are you grabbing the data from the form? In order to process
your input, you need to grab the data from the form INPUT fields. Also
your Booking array is not declared and has no data. Below is what I
would do:

function validate_booking() {

var Booking = new Array(9); // declare your array with a size
var i = parseInt(document.newbieFrm.bookingno.value); // grab input data

// populate your Booking array with data

Booking[0] = 5;
Booking[1] = 8;
 
L

Laurent Bugnion, GalaSoft

Hi,

orlando said:
Several things going on here:

1 - this statement:

var j = 1

should be:

var j = 1;

To be totally complete, and while I also recommend to finish each
statement with a semi-colon, it is not compulsory in JavaScript. A
statement may be ended by a semi-colon or by a line feed. Semi-colons
are thus compulsory only when more than one statement is found on the
same line, for example in a "for" loop:

for ( var index = 0; index < 5; index++ )
{
}

However, it is good practice to be consequent and to always use
semi-colons to terminate each statement (as is compulsory in other
C-like languages), it also increases the code's readibility, which is
always good.

HTH,

Laurent
 
L

Lasse Reichstein Nielsen

Laurent Bugnion said:
To be totally complete, ....
Semi-colons
are thus compulsory only when more than one statement is found on the
same line, for example in a "for" loop:
for ( var index = 0; index < 5; index++ )

Since we are in pedantic mode, the semicolons in a for statement are
not statement terminators, but are part of the syntax of the for
statement.

Semicolons can be omitted at the end of *some* lines in Javascript,
not all. Compare the results of:
 
L

Laurent Bugnion, GalaSoft

Hi,
Since we are in pedantic mode,

I resent that *LOL*

the semicolons in a for statement are
not statement terminators, but are part of the syntax of the for
statement.

I am not so sure about that. If you debug JavaScript code (or another
C-like language for that matter), you'll see that the debugger steps in
the first "statement" "var index = 0;" (Creation and allocation of
index), then on the second "index < 5" (where it checks if the condition
is still true) and then on the third one (where is performs the
requested action, in this case increase index by one unit).

Don't you call this "statements"?

<snip>

Laurent
 
L

Lasse Reichstein Nielsen

Laurent Bugnion said:
I am not so sure about that. If you debug JavaScript code (or another
C-like language for that matter), you'll see that the debugger steps
in the first "statement" "var index = 0;" (Creation and allocation of
index), then on the second "index < 5" (where it checks if the
condition is still true) and then on the third one (where is performs
the requested action, in this case increase index by one unit).

Yes, it evaluates the expressions.
Don't you call this "statements"?

No. A for statment is an Iterationstatement. It has the following
production in the syntax.
IterationStatement :
for ( ExpressionNoIn_opt ; Expression_opt ; Expression_opt ) Statement

The three parts of the for control logic are expressions, not statements.
They are *separated* by semicolons, which are not statement-terminating
semicolons as introduced by the productions:
ExpressionStatement :
[lookahead not in { {, function}] Expression ;
and
EmptyStatement :
;


It would be illegal to write
for (var i=0;i<10;i++;) { ... }
i.e., having a semicolon after the third expression. It would not
be illegal if they were statments.

The expressions in a for statement are no more statements than
the expression in the condition of an if statement.

/L
 
L

Laurent Bugnion, GalaSoft

Hi,
I am not so sure about that. If you debug JavaScript code (or another
C-like language for that matter), you'll see that the debugger steps
in the first "statement" "var index = 0;" (Creation and allocation of
index), then on the second "index < 5" (where it checks if the
condition is still true) and then on the third one (where is performs
the requested action, in this case increase index by one unit).


Yes, it evaluates the expressions.

Don't you call this "statements"?


No. A for statment is an Iterationstatement. It has the following
production in the syntax.
IterationStatement :
for ( ExpressionNoIn_opt ; Expression_opt ; Expression_opt ) Statement

The three parts of the for control logic are expressions, not statements.
They are *separated* by semicolons, which are not statement-terminating
semicolons as introduced by the productions:
ExpressionStatement :
[lookahead not in { {, function}] Expression ;
and
EmptyStatement :
;


It would be illegal to write
for (var i=0;i<10;i++;) { ... }
i.e., having a semicolon after the third expression. It would not
be illegal if they were statments.

The expressions in a for statement are no more statements than
the expression in the condition of an if statement.

/L

OK, everyday one learns something new.

Thanks,

Laurent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top