Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Variables not showing correctly in HTML

Variables not showing correctly in HTML

Scheduled Pinned Locked Moved JavaScript
javascripthtmltoolshelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tristarterror
    wrote on last edited by
    #1

    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.

    D 1 Reply Last reply
    0
    • T tristarterror

      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.

      D Offline
      D Offline
      dusty_dex
      wrote on last edited by
      #2

      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"

      T 1 Reply Last reply
      0
      • D dusty_dex

        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"

        T Offline
        T Offline
        tristarterror
        wrote on last edited by
        #3

        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!

        D 1 Reply Last reply
        0
        • T tristarterror

          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!

          D Offline
          D Offline
          dusty_dex
          wrote on last edited by
          #4

          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:

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups