Variables not showing correctly in HTML
-
I am currently a college student so please be kind to me being a novice! I am working on an assignment and I just can't figure out where I am going wrong. Assignment Details: Function: tst_name Parameter: A string. Returns: true if the given string has the form String1, String2 letter where both strings must be all lowercase letter except for the first letter and letter must be uppercase; false otherwise My JavaScript file:
function tst_name(name) {
// simple pattenr to check the format of the name
var ok = name.search(/^[A-Z][a-z]+, [A-Z][a-z]+, [A-Z]\.?$/);if (ok == 0) { return true; } else { return false; }
} // end of function tst_name
// Test function with two sets of data
var tst = tst_name(Woods, Trista, M);
if (tst) {
document.write("Program error <br />");
} else {
document.write("Valid name. <br />");
}var tst = tst_name(woods, trista, m);
if (tst) {
document.write("Invalid name <br />");
} else {
document.write("Program error <br />")
}My HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Woods | Test Name | Exercise 4.12</title>
<script type="text/javascript" src="exercise4-12.js">
</script>
</head>
<body>
<script>
document.write("Woods, Trista, M: " + tst + "<br />");
document.write("woods, trista, m: " + tst + "<br />");
</script>
</body>
</html>When I view this in a browser it just shows: Woods, Trista, M: undefined woods, trista, m: undefined So I'm just confused. I followed an example in our book and I just don't know what I need to do in order to get the variables to write the correct statements in my HTML document.
-
I am currently a college student so please be kind to me being a novice! I am working on an assignment and I just can't figure out where I am going wrong. Assignment Details: Function: tst_name Parameter: A string. Returns: true if the given string has the form String1, String2 letter where both strings must be all lowercase letter except for the first letter and letter must be uppercase; false otherwise My JavaScript file:
function tst_name(name) {
// simple pattenr to check the format of the name
var ok = name.search(/^[A-Z][a-z]+, [A-Z][a-z]+, [A-Z]\.?$/);if (ok == 0) { return true; } else { return false; }
} // end of function tst_name
// Test function with two sets of data
var tst = tst_name(Woods, Trista, M);
if (tst) {
document.write("Program error <br />");
} else {
document.write("Valid name. <br />");
}var tst = tst_name(woods, trista, m);
if (tst) {
document.write("Invalid name <br />");
} else {
document.write("Program error <br />")
}My HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"
>
<html lang="en">
<head>
<title>Woods | Test Name | Exercise 4.12</title>
<script type="text/javascript" src="exercise4-12.js">
</script>
</head>
<body>
<script>
document.write("Woods, Trista, M: " + tst + "<br />");
document.write("woods, trista, m: " + tst + "<br />");
</script>
</body>
</html>When I view this in a browser it just shows: Woods, Trista, M: undefined woods, trista, m: undefined So I'm just confused. I followed an example in our book and I just don't know what I need to do in order to get the variables to write the correct statements in my HTML document.
-
Your test script is attempting to pass three variables (which don't exist) named Woods, Trista and M. Ditto for the lower case as well. surround them with quotes. "Woods, Trista M"
Oh geez! It's always the simple things that screw me up. I also noticed I was missing a semicolon after one of my statements too. Thanks for helping a beginner out!
-
Oh geez! It's always the simple things that screw me up. I also noticed I was missing a semicolon after one of my statements too. Thanks for helping a beginner out!
tristarterror wrote:
Thanks for helping a beginner out!
No problem. Semicolons in javascript are a bit of a nuisance because there are some nasty surprises with return statements in particular. Two books you definitely ought to have handy. Javascript the Definitive Guide 5th edition (O'Reilly), and Douglas Crockford's Javascript The Good Parts (O'Reilly). The language is a b*stard of a mess. Even the original 'official' language specification is crap. :mad: