...

View Full Version : Help with Arrays



TehMadHatter
01-19-2012, 07:13 AM
<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
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?

Old Pedant
01-19-2012, 08:23 AM
Ummm...see the line in red?


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


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:


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


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.

Old Pedant
01-19-2012, 08:31 AM
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:


//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, "" );
}
}

felgall
01-19-2012, 08:56 AM
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.

TehMadHatter
01-19-2012, 04:31 PM
Then what do you use instead to write to the document? And to ask for an input with prompt?

Philip M
01-19-2012, 04:44 PM
The answer is DOM.


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>

TehMadHatter
01-19-2012, 06:23 PM
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?

Old Pedant
01-19-2012, 08:16 PM
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?

Old Pedant
01-19-2012, 08:18 PM
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.

TehMadHatter
01-19-2012, 08:52 PM
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?

Old Pedant
01-19-2012, 09:03 PM
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.

TehMadHatter
01-19-2012, 09:52 PM
I have gotton an example from the end of part 2, of this course...


<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?

Old Pedant
01-20-2012, 12:22 AM
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:


<!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:


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum