View Full Version : to chose two conditional alertnatives
hughesmi
09-26-2002, 05:10 PM
what I'm doing wrong?
please help
<html>
<head>
<title></title>
</head>
<body>
<Script Language="JavaScript">
function test_age()
{
if (age>64) {
document.write("Welcome, Senior Citizen");
seniors=seniors+1;
}
else {
document.write("Welcome, Youngster");
young=young+1;
}
}
</script>
<input type="button" value="Click" onclick="test_age();">
<br>
<br>
</html>
How does the script know what age the person is who's clicking the button? That value must be some where but in the script that you have included, I do not see that any where.
beetle
09-26-2002, 10:27 PM
yes, where is the variable age coming from?
Also, you can use the increment operator (++) in place of these statements
Replace:
seniors=seniors+1;
young=young+1;
With:
seniors++;
young++;
chrismiceli
09-26-2002, 10:36 PM
i prefer
++senior and ++yongerster because it works with subtractions signs that way
--senior and --younster
but they both work in this case
to get it to work ad something like this in there
age = prompt("What is your age", "Enter your age here");
beetle
09-26-2002, 11:07 PM
senior--;
young--;
is perfectly valid.
mordred
09-27-2002, 12:00 AM
+ keep in mind that --senior and senior-- work a little bit differently as to when the value of senior is returned.
beetle
09-27-2002, 12:55 AM
Right, like to increment an arrayvar i = 0;
var a = new Array()
a[i++] = "val";
a[i++] = "val";versusvar i = 0;
var a = new Array()
a[i] = "val";
a[++i] = "val";In otherwords, ++i increments i BEFORE returning it's value, and i++ increments i AFTER returning it's value.
hughesmi
09-27-2002, 12:28 PM
I'm confused?
Can you explain to me where, the
seniors=seniors+1;
and
young=young+1;
come in to play with my script.
I can't see how this works?
I'm trying to undrstand how, to chose conitionally between two alternatives.
i.e
if (age>64) {
document.write("welcome, senior citizen");
seniors=seniors+1;
}
else {
document.write("welcome, youngster");
young=young+1;
}
document.write("seniors= "+seniors+"<br>");
document.write("young= "+young+"<br>");
RadarBob
09-27-2002, 02:55 PM
These guys are just going off on a tangent...
Your basic conditional statement looks ok, but as stated above the "age" value has to be put into the function so the conditional statement can do it's thing! So it might look like this:
function test_age(age) {
if (age>64) {
document.write("Welcome, Senior Citizen");
seniors=seniors+1;
}else{
document.write("Welcome, Youngster");
young=young+1;
}
}
So now you need a text box, radio button, something, so the user can tell your program what category (s)he is in. It might look like this:
<input type="text" name="ageInput" value'="" onblur="test_age(this);">
beetle
09-27-2002, 03:11 PM
Actually, RadarBob, with how you have that coded, should be this<input type="text" name="ageInput" value'="" onblur="test_age(this.value);">
RadarBob
09-27-2002, 04:46 PM
Opps, ya, you're right. Just tired. Sorry.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.