12 Replies - 1423 Views - Last Post: 20 November 2012 - 08:13 AM

#1 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Arrays issue

Posted 13 November 2012 - 07:47 AM

Create a mini user information management system that asks the user to enter an employee's first name, last name, and salary. When the user clicks on a Confirm button, the information entered will be displayed to the user cummulatively. E.g. if the user enters the first employee, and clicks on the button, one employee information will be displayed. If the user enters another employee and clicks on the button, the information for two employees will be displayed. Hint: you will need to create three arrays and assign values to them.

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays issue

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Arrays issue

Posted 13 November 2012 - 07:48 AM

what have you tried?
Was This Post Helpful? 0
  • +
  • -

#3 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Arrays issue

Posted 13 November 2012 - 07:53 AM

I have set up the basic structure that I think i need but just never done an array before ill show you my structure for now and we can build off of that

<!doctype.html>
	<!-- Lab #11 Arrays -->	 
	<html>
	    <head>
	        <title> Arrays </title>
	    <script type="text/javascript">
   function mine(){
				var bin= parseInt(document.getElementById("naming").value)
				var cont= parseInt(document.getElementById("naming2").value)
				var hold= parseInt(document.getElementById("naming3").value)
				
	            
	     
	    </script>
	    </head>
     
	    <body>
	     
	         
	        <div id="outputDiv">
	         
         
	        <h1 style="text-align:center"> For Loop </h1>
	            <br>
	          First Name: <input type="text" id="naming" size=14 value="">
	          <br>
	          Last Name:<input type="text" id="naming2" size=14 value="">
			  <br>
			  Salary:<input type="text" id="naming3" size=14 value="">
          <input type="button" value="Confirm" onclick="mine()"/>
	        </div>
	        </body>
	</html>



Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Arrays issue

Posted 13 November 2012 - 08:03 AM

why do you convert the employee names into numbers?
Was This Post Helpful? 0
  • +
  • -

#5 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Arrays issue

Posted 13 November 2012 - 08:05 AM

sorry i have set it up again
<!doctype.html>
	<!-- Lab #11 Arrays -->	 
	<html>
	    <head>
	        <title> Arrays </title>
	    <script type="text/javascript">
				var first= new array():
				var last= new array():
				var salary= new array():
  function mine()
				
	            
	     
	    </script>
	    </head>
     
	    <body>
	     
	         
	        <div id="outputDiv">
	         
         
	        <h1 style="text-align:center"> Array with names and salaries </h1>
	            <br>
	          First Name: <input type="text" id="naming" size=14 value="">
	          <br>
	          Last Name:<input type="text" id="naming2" size=14 value="">
			  <br>
			  Salary:<input type="text" id="naming3" size=14 value="">
          <input type="button" value="Confirm" onclick="mine()"/>
	        </div>
	        </body>
	</html>


Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Arrays issue

Posted 13 November 2012 - 08:10 AM

first, actually this set-up is inside-out. (more on that later)

second, nowhere in the assignment is said, you should use an array or actually store the information.

Quote

When the user clicks on a Confirm button, the information entered will be displayed to the user cummulatively

you are supposed to display the data!
Was This Post Helpful? 0
  • +
  • -

#7 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Arrays issue

Posted 13 November 2012 - 09:24 AM

Well we discussed that that is part of the lab so array has to be used
Was This Post Helpful? 0
  • +
  • -

#8 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

Re: Arrays issue

Posted 13 November 2012 - 09:34 AM

Well.. You haven't really used arrays at all yet(each of those lines of javascript will produce an error).

What have you learned about arrays so far? This might be a good place to start.

This post has been edited by Sho Ke: 13 November 2012 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

#9 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Arrays issue

Posted 13 November 2012 - 09:42 AM

i have learned like the basics but havent understood the concepts structure is really all ik
Was This Post Helpful? 0
  • +
  • -

#10 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

Re: Arrays issue

Posted 13 November 2012 - 10:09 AM

Well if structure is all you've done with arrays, you may want to start with trying to display a single value using arrays before jumping into the assignment head on.

Think about how you could display an input field's value using an alert box. That might look something like:
<html>
	<head>
		<script>
			function getName() {
				var name = document.getElementById("name").value;
				alert(name);
			}
		</script>
	</head>
	<body>
		<input type="text" id="name" />
		<input type="button" onclick="getName()" />
	</body>
</html>



In the above code, you're creating a variable name and assigning it the value of an input field. You can achieve the exact same result using arrays, like so:
<html>
	<head>
		<script>
			function getName() {
				var name = new Array();
				
				//Remember that arrays, by default, start at 0, not 1
				name[0] = document.getElementById("name").value;
				alert(name[0]);
			}
		</script>
	</head>
	<body>
		<input type="text" id="name" />
		<input type="button" onclick="getName()" />
	</body>
</html>



And with that, all that's really left is just to find a way to save the values of the input fields(first name, last name, salary) each time the button is clicked so that when you enter the second employee's information and press the button again, both employee's information appears instead of only the second one.

Just remember that at the end of a function, all variables that were created within it are destroyed, so you'll most likely need to use a global variable.
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Arrays issue

Posted 13 November 2012 - 01:13 PM

<rant>

with regards to the assignment itself, I’d say this is a very bad example when it comes to demonstrating arrays. when I read it, my insides were screaming "custom objects", which, admittedly, are far too advanced at this point. theoretically it were possible to save these Employee objects in an array, but the assignment does not make any demands at a storage at all, despite in the hint, which is IMHO misleading and will enforce bad practice (keyword: data integrity [1]). You will find it awfully difficult to unlearn the bad practices you learned in the very beginning.

</rant>

[1] - giving an example what I mean by data integrity. assuming you have the following data:
var firstNames = ["john", "paul"];
var lastNames = ["doe", "sartre"];

// so you could get a full name via a function
function fullName(num)
{
    return firstNames[num] + " " + lastNames[num];
}

alert( fullName(1) ); // paul sartre

// now for some odd reason we modify one of the arrays
var name1 = firstNames.shift();

// now we display another name
alert( fullName(0) ); // paul doe

// wtf? it should return "john doe" !

// this is a typical example of failed data integrity 
// and the problems that come with global variables

This post has been edited by Dormilich: 13 November 2012 - 01:13 PM

Was This Post Helpful? 2
  • +
  • -

#12 ghukill88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 08-November 12

Re: Arrays issue

Posted 20 November 2012 - 07:43 AM

this is what i have taken from your advise what will I do next?

<html>

    <head>

        <script>
           var firstnames = ["mike","gage"];
		   var lastnames = ["klein","hukill"];
		   var salary2 = ["25000","30000"]
		   function getName() {
			var name= new Array();
			var last= new Array();
			var salary = new Array();
				name[0] = document.getElementById("name").value;
				last[1] = document.getElementById("last").value;
				salary[2] = document.getElementById("salary").value;
              
            }

        </script>

    </head>

    <body>

        <input type="text"  id="name" />
		<br>
		 <input type="text"  id="last" />
		 <br>
		  <input type="text"  id="salary" />

        <input type="button" value="confirm" onclick="getName()" />

    </body>

</html>



Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3404
  • View blog
  • Posts: 9,625
  • Joined: 08-June 10

Re: Arrays issue

Posted 20 November 2012 - 08:13 AM

delete it.

the problem with the task is (according to the hint) that the solution is susceptible to data integrity failures. the obvious (at least to advanced coders) solution is to group those three data item together, which is usually done through objects, but that is far too advanced at this stage.

the only possibility I see is to port the object approach to arrays, which must sacrifice some of the usefulness.

so, what we need is a group. what you can do (at this level) is use an array. index 0 for the first name, index 1 for last name and index 2 for salary. this array you can then put in the "global" storage array.

I’ll demonstrate the object approach
// don’t wonder why I do it this way
function Employee(first, last, money)
{
    var salary = null;
    Object.defineProperty(this, "salary", {
        get: function ()  { return salary; },
        set: function (s) { salary = Employee.toNumber(s); },
        enumerable: true
    });
    this.salary = money;
    Object.defineProperty(this, "firstName", {
        value: first, 
        enumerable: true
    });
    Object.defineProperty(this, "lastName",  {
        value: last,
        enumerable: true
    });
}
Employee.prototype.toString = function ()
{
    return this.firstName + " " + this.lastName;
};
Employee.toNumber = function (x)
{
	x = parseFloat(x);
	if (isNaN(x)) {
		throw new TypeError("Salary is not a number.");
	}
	return x;
};

// next we create an Employee object from our HTML form
// (skipping how you get the values, using strings directly)

// create new array
var workers = [];
// create employee and add to array
workers.push(new Employee("John", "Doe", 10000));
// add another employee
workers.push(new Employee("Jean-Paul", "Sartre", 20000));

// alert 1st employee
alert( workers[0] ); // John Doe
// delete John Doe
workers.shift();
// alert 1st employee
alert( workers[0] ); // Jean-Paul Sartre
// modify his salary
workers[0].salary = "none"; // Error, salary is not a number (error console)
workers[0].salary = 25000;
// check the salary
alert(workers[0].salary);
// and there is a lot more magic I can do with that object
// rename?
workers[0].firstName = "Mary";
alert(workers[0]); // John Doe
alert(workers[0].firstName); // John

This post has been edited by Dormilich: 21 November 2012 - 03:12 AM
Reason for edit:: some code optimisation

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1