Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Arrays

    Code:
    <HTML>
    
    <HEAD>
    <TITLE>Listing 4.4</TITLE>
    
    <SCRIPT TYPE="text/javascript">
    
    //DEFINE METHOD
    function displayInfo() {
      document.write("<H1>Employee Profile: " + this.data[0] + "</H1><HR /><PRE>");
      document.writeln("Employee Number: " + this.data[1]);
      document.writeln("Social Security Number: " + this.data[2]);
      document.writeln("Annual Salary: " + this.data[3]);
      document.write("</PRE>");
    }
    
    //DEFINE METHOD TO GET EMPLOYEE INFORMATION
    function getInfo() {
      var menu="0-Exit/1-Name/2-Emp. #/3-Soc. Sec. #/4-Salary";
      var choice=prompt(menu,"0");
      if (choice != null) {
        if ((choice < 0) || (choice > 4)) {
          alert ("Invalid choice");
          this.getInfo();
        } else {
          if (choice != "0") {
            this.data[choice-1]=prompt("Enter information","");
            this.getInfo();
          }
        }
      }
    }
    
    //DEFINE OBJECT
    function employee() {
      this.data = new Array(4);
      this.displayInfo=displayInfo;
      this.getInfo=getInfo;
    }
    
    newEmployee=new employee();
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <SCRIPT TYPE="text/javascript">
    
    newEmployee.getInfo();
    newEmployee.displayInfo();
    
    </SCRIPT>
    
    </BODY>
    
    </HTML>

    I am currently taking a Javascript certification course, this is a exercise given (final code)

    I don't completely understand what is going on though, could someone explain to me
    Code:
     this.data[choice-1]=prompt("Enter information","");
    that part? I don't understand why, [choice-1]

    I don't fully understand why an array is even needed here?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Ummm...see the line in red?
    Code:
    function employee() {
      this.data = new Array(4);
      this.displayInfo=displayInfo;
      this.getInfo=getInfo;
    }
    That's there because there are 4 pieces of information about each employee that you need to collect.

    And the four pieces are pretty well laid out here
    Code:
    function displayInfo() {
      document.write("<H1>Employee Profile: " + this.data[0] + "</H1><HR /><PRE>");
      document.writeln("Employee Number: " + this.data[1]);
      document.writeln("Social Security Number: " + this.data[2]);
      document.writeln("Annual Salary: " + this.data[3]);
      document.write("</PRE>");
    }
    But for some reason, the choices are numbered ONE OFF from the positions of the data in the array:
    Code:
      var menu="0-Exit/1-Name/2-Emp. #/3-Soc. Sec. #/4-Salary";
    That is, choice #4 is Salary, and yet Salary is clearly data[3], right?

    So...you type in a 4 for the first prompt and then the second prompt will (presumably) get the salary you input an put it into data[choice-1] here
    Code:
        data[choice-1] = prompt("Enter information","");
    and since choice was 4, salary goes into choice[3].

    It is, quite frankly, a crappy design. Haphazard at best.

    What's to prevent a user from entering only a salary (no name, employee number, or social security number)? That code will happily accept the salary alone and then report no values at all for the other characteristics.

    I would certify this course as not the best choice, if this is indicative of what they are teaching. It certainly isn't teaching you to create *usable* javascript code. In my never overly humble opinion.
    Last edited by Old Pedant; 01-19-2012 at 08:26 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    I would personally never teach a JavaScript course that had a "final code" that still used prompt and document.write. But at a minimum, I would have written the one function this way:
    Code:
    //DEFINE METHOD TO GET EMPLOYEE INFORMATION
    function getInfo() {
        var menu= ["name","employee number","social security number","salary"];
        for ( var m = 0; m < menu.length; ++m )
        {
            if ( m == 0 ) { ename = "this employee"; } else { ename = data[0]; }
            data[m] = prompt( "Please enter the " + menu[m] + " for " + ename, "" );
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,472
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by Old Pedant View Post
    I would personally never teach a JavaScript course that had a "final code" that still used prompt and document.write.
    I personally do teach a JavaScript course and at no point in the course do I ever suggest that the student should ever use either of those antiquated obsolete commands. In fact the only mention they get is when I tell the students that any script using either needs to be thrown away and rewritten in JavaScript (as neither statement is a part of Modern JavaScript and I teach programming not history in that class). I do supply reasons why you shouldn't use those statements in the course notes - see http://javascriptexample.net/badjs.php for the part of the notes that covers those parts of JavaScript and other similar commands that shouldn't be used in at least 99.99999% of the cases where those who haven't learnt JavaScript properly use them.

    It isn't even necessary to use those statements to teach JavaScript and anyone who does teach using those statements should be a student not a teacher as they first need to learn JavaScript before they can meaningfully teach it. The only statement that is an exception for teaching is alert since showing the result of running a partial script is not all that different from showing the value in order to debug the script - which is what alert is for.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Then what do you use instead to write to the document? And to ask for an input with prompt?

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    The answer is DOM.

    Code:
    What is your name? <input type = "text" id = "myname" onblur = "show()">
    <br><br>
    <span id = "result"></span>
    
    <script type = "text/javascript">
    
    function show() {
    var x = document.getElementById("myname").value;
    document.getElementById("result").innerHTML = "Your name is " +  x;
    }
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah I see...Well I think maybe that is coming still in the class, I am still on just part 1.

    Maybe they were just showing prompt and docuemnt.write just to show it ya know?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by TehMadHatter View Post
    Maybe they were just showing prompt and docuemnt.write just to show it ya know?
    But read what Felgall said. Those are obsolescent and should, in general, never be used in any modern JavaScript coding.

    We both agree that teaching them to you is more a waste of your time than anything.

    It's not that hard to learn to correctly mix HTML and JavaScript and use the DOM.

    Why waste even one week of class on document.write and prompt?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    What's especially funny is that they *are* teaching you how to create an object. If you weren't told, your function employee() is a constructor for an object. And that's quite reasonable and actually is beyond what some beginning JS classes teach in the early going. So this course seems to be a hodgepodge mix of good and bad techniques.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well yeah I have already learned functions, objects, variables, and expressions..

    From what you people are telling me though it makes me wonder if this course is even worth it? If they are teaching me obsolete commands?

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    If they are teaching you functions, objects, etc., then it's not completely a bad course. Stick with it. Just be aware that they aren't teaching you (YET! hope that changes) how to really *USE* JavaScript in conjunction with HTML, which is the primary usage in the real world.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    New Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have gotton an example from the end of part 2, of this course...

    Code:
    <HTML>
    
    <HEAD>
    <TITLE>Listing 6.9</TITLE>
    
    <SCRIPT TYPE="text/javascript">
    
    function clearField(field) {
    
      // Check if field contains the default value
      if (field.value == field.defaultValue) {
    
        // It does, so clear the field
        field.value = "";
      }
    
    }
    
    function checkField(field) {
    
      // Check if user has entered information in the field
      if (field.value == "") {
    
        // User has not entered anything
        field.value = field.defaultValue;
      }
    
    }
    
    </SCRIPT>
    
    </HEAD>
    
    <BODY>
    
    <FORM METHOD="POST">
    <INPUT TYPE="text" NAME="name" VALUE="Name"
           onFocus="clearField(this);"
           onBlur="checkField(this);" />
    <BR />
    <INPUT TYPE="text" NAME="email" VALUE="E-mail Address"
           onFocus="clearField(this);"
           onBlur="checkField(this);" />
    <BR />
    <INPUT TYPE="text" NAME="phone" VALUE="Phone Number"
           onFocus="clearField(this);"
           onBlur="checkField(this);" />
    </FORM>
    
    </BODY>
    
    </HTML>

    Does this seem like what you were saying upon more modern javascript that is used in thereal world? Or closer?

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    well, yes, the JavaScript is better.

    But now the HTML is out of date.

    Most people nowadays use something like this at the top of their html pages:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
    and even xhtml transitional insists on all lower case tag names and attribute names. Example:
    Code:
    <input type="text" name="phone" value="Phone Number"
          onfocus="clearField(this);'
          onblur = "checkField(this);" />
    In fact, it's strange to see upper case tag names such as <INPUT that then end with /> instead of the old-fashioned >. So that HTML seems to be a bit of a hodge-podge, too.

    Fortunately, all current browser are pretty forgiving of stuff like this. Plus you can easily validate your HTML using the w3c validator, to help make it compliant with the standards.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •