PDA

View Full Version : Javascript beginner if-then question


lifeincolour
11-24-2002, 07:09 PM
Hi... I'm messing about with a simple code that finds out if you're Mr, Mrs, Master or Miss and then writes it to the page, as so...

name=prompt("What is your second name?","Name");
sex=prompt("Are you a man or a woman?","man/woman");
marital=prompt("Are you single or married?","unmarried/married");

if (sex=="man" && marital=="unmarried") {fulltitle="Master"};
if (sex=="man" && marital=="married") {fulltitle="Mr"};
if (sex=="woman" && marital=="unmarried") {fulltitle="Miss"};
if (sex=="woman" && marital=="married") {fulltitle="Mrs"};

document.write(fulltitle);
document.write(" ");
document.write(name);

But how would I add a piece of code that would write different messages to the page if

a) neither "man" nor "woman" was typed in correctly
b) neither "married" nor "unmarried" was typed in correctly?

chrismiceli
11-24-2002, 07:57 PM
if (sex != man && sex != woman) {
alert("you mistyped it")
}
if (marital != married && marital != unmarried) {
alert("you mistyped it")
}

lifeincolour
11-24-2002, 07:59 PM
Aha! Thanks mate
Chloe

redhead
11-24-2002, 08:38 PM
or just put:

else { alert('You cannot spell.'); };

at the end of all them if {}; 's