4 Replies - 2682 Views - Last Post: 17 April 2008 - 03:47 PM

#1 meditate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-February 07

Salary

Posted 16 April 2008 - 06:40 PM

Problem:
A company pays its salespeople on commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. (i.e. if someone grosses $5000 dollars in sales in a week they receive 200 + 9% of 5000, or a total of 650.) I am supposed to write a script using an array of counters that obtains the gross sales for each employee through an XHTML form and determine how many of the salespeople earned salaries in each of the following range.

My solution:
<head>
	<script type="text/javascript">
		<!--
		var rangeOfsales = new Array( 9 );
		var salary = 200;
		var grossSales = 0;
		var commission = 0;




		function calculate(grossSales)
		{
			commission = grossSales * .09;
			salary = salary + commission;
		
			if(salary >= 200 && salary <= 299)
				rangeOfsales[0]++;
			if(salary >= 300 && salary <= 399)
				rangeOfsales[1]++;
			if(salary >= 400 && salary <= 499)
				rangeOfsales[2]++;
			if(salary >= 500 && salary <= 599)
				rangeOfsales[3]++;
			if(salary >= 600 && salary <= 699)
				rangeOfsales[4]++;
			if(salary >= 700 && salary <= 799)
				rangeOfsales[5]++;
			if(salary >= 800 && salary <= 899)
				rangeOfsales[6]++;
			if(salary >= 900 && salary <= 999)
				rangeOfsales[7]++;
			if(salary >= 1000)
				rangeOfsales[8]++;
		}

		function createText()
 		{
			myForm.result.value="Number of people who earned salaries in the following ranges:\n"+
					"$200-$299\t\t" + rangeSales[0] + "\n" +
					"$300-$399\t\t" + rangeSales[1] + "\n" +
					"$400-$499\t\t" + rangeSales[2] + "\n" +
					"$500-$599\t\t" + rangeSales[3] + "\n" +
					"$600-$699\t\t" + rangeSales[4] + "\n" +
					"$700-$799\t\t" + rangeSales[5] + "\n" +
					"$800-$899\t\t" + rangeSales[6] + "\n" +
					"$900-$999\t\t" + rangeSales[7] + "\n" +
					"$1000 and up\t\t" + rangeSales[8];
					
 		}
		// -->
</script>
</head>

<body>
	<form name = "myForm" action = "">

		<p> 
			Enter Sales Amount: <br />
			<input name = "input" type = "text" />
			<input name = "Submit" type = "button" value =
				"Submit" onclick = "calculate(myForm.input.value)" /><br />
		</p>
		<p>
			<textarea name = "result" rows = "11" cols = "60">
			</textarea>
		</p>
	</form>
</body>
</html>



This is pretty much my full code. It isn't actually printing anything to the text box and does nothing when I hit submit. What can i do to fix this code?

Is This A Good Question/Topic? 0
  • +

Replies To: Salary

#2 fahlyn  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 03-November 07

Re: Salary

Posted 16 April 2008 - 07:55 PM

it looks like you've got a few things wrong.

#1: your createText() method isn't ever getting called.
#2: your rangeSales array that is used in your createText() method is never defined....i think you mean it to be rangeOfsales.
#3: the way you have this setup only one of those 9 values in rangeOfSales will ever get set...because a salary value won't ever be between 200 & 299 and also 300 and 399...in addition to that...your rangeOfSales array is defined as an array of 9 objects...but those objects are never initialized to anything...so the ++ operation is going to try to increment "undefined" which will probably end up with NAN as a result.

I think if you knock out those problems, you'll be on the right track.

Happy Coding.
Was This Post Helpful? 0
  • +
  • -

#3 meditate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-February 07

Re: Salary

Posted 16 April 2008 - 08:21 PM

<head>
	<title> Chapter 11 - java script: Arrays - Exercise 11.10 - Page 374 - Homework Assignment - April 16, 2008 </title>
	<meta http-equiv="content-type" 
		content="text/html;charset=utf-8" />
	<meta http-equiv="Content-Style-Type" content="text/css" />

	
	<script type="text/javascript">
		<!--
		var rangeOfsales = new Array( 9 );
		var salary = 200;
		var grossSales = 0;
		var commission = 0;

		for (var i = 0; i < rangeOfsales.length; ++i)
			rangeOfsales[ i ] = 0;


		function calculate(grossSales)
		{
			commission = grossSales * .09;
			salary = salary + commission;
		
			if(salary >= 200 && salary <= 299)
				rangeOfsales[0]++;
			if(salary >= 300 && salary <= 399)
				rangeOfsales[1]++;
			if(salary >= 400 && salary <= 499)
				rangeOfsales[2]++;
			if(salary >= 500 && salary <= 599)
				rangeOfsales[3]++;
			if(salary >= 600 && salary <= 699)
				rangeOfsales[4]++;
			if(salary >= 700 && salary <= 799)
				rangeOfsales[5]++;
			if(salary >= 800 && salary <= 899)
				rangeOfsales[6]++;
			if(salary >= 900 && salary <= 999)
				rangeOfsales[7]++;
			if(salary >= 1000)
				rangeOfsales[8]++;

			output();
		}

		function output()
 		{
			myForm.result.value="Number of people who earned salaries in the following ranges:\n"+
					"$200-$299\t\t" + rangeOfSales[0] + "\n" +
					"$300-$399\t\t" + rangeOfSales[1] + "\n" +
					"$400-$499\t\t" + rangeOfSales[2] + "\n" +
					"$500-$599\t\t" + rangeOfSales[3] + "\n" +
					"$600-$699\t\t" + rangeOfSales[4] + "\n" +
					"$700-$799\t\t" + rangeOfSales[5] + "\n" +
					"$800-$899\t\t" + rangeOfSales[6] + "\n" +
					"$900-$999\t\t" + rangeOfSales[7] + "\n" +
					"$1000 and up\t\t" + rangeOfSales[8];
					
 		}
		// -->
</script>
</head>

<body>
	<form name = "myForm" action = "">

		<p> 
			Enter Sales Amount: <br />
			<input name = "input" type = "text" />
			<input name = "Submit" type = "button" value =
				"Submit" onclick = "calculate(myForm.input.value)" /><br />
		</p>
		<p>
			<textarea name = "result" rows = "11" cols = "60">
			</textarea>
		</p>
	</form>
</body>
</html> 


I renamed createText() method to output() and called it.
I renamed rangeSales in output() to rangeOfsales
I initialized the elements in my array.

When i type 5000 it is still not adding a 1 to the 600-699 range in my text area. In fact nothing is printed to my text area at all.

The array acts as a counter to how many employees made between those ranges of money.

This post has been edited by meditate: 16 April 2008 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 meditate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-February 07

Re: Salary

Posted 16 April 2008 - 10:33 PM

 
<head>
	<script type="text/javascript">
		<!--
		var rangeOfsales = new Array( 9 );
		var salary = 0;
		var grossSales = 0;
		var commission = 0;

		for (var i = 0; i < rangeOfsales.length; ++i)
			rangeOfsales[ i ] = 0;


		function calculate(grossSales)
		{
			commission = grossSales * .09;
			salary = 200 + commission;
		
			if(salary >= 200 && salary <= 299)
				rangeOfsales[0] += 1;
			if(salary >= 300 && salary <= 399)
				rangeOfsales[1] += 1;
			if(salary >= 400 && salary <= 499)
				rangeOfsales[2] += 1;
			if(salary >= 500 && salary <= 599)
				rangeOfsales[3] += 1;
			if(salary >= 600 && salary <= 699)
				rangeOfsales[4] += 1;
			if(salary >= 700 && salary <= 799)
				rangeOfsales[5] += 1;
			if(salary >= 800 && salary <= 899)
				rangeOfsales[6] += 1;
			if(salary >= 900 && salary <= 999)
				rangeOfsales[7] += 1;
			if(salary >= 1000)
				rangeOfsales[8] += 1;

			output();
		}

		function output()
 		{
			myForm.result.value="Number of people who earned salaries in the following ranges:\n"+
					"$200-$299\t\t" + rangeOfSales[0] + "\n" +
					"$300-$399\t\t" + rangeOfSales[1] + "\n" +
					"$400-$499\t\t" + rangeOfSales[2] + "\n" +
					"$500-$599\t\t" + rangeOfSales[3] + "\n" +
					"$600-$699\t\t" + rangeOfSales[4] + "\n" +
					"$700-$799\t\t" + rangeOfSales[5] + "\n" +
					"$800-$899\t\t" + rangeOfSales[6] + "\n" +
					"$900-$999\t\t" + rangeOfSales[7] + "\n" +
					"$1000 and up\t\t" + rangeOfSales[8];
					
 		}
		// -->
</script>
</head>

<body>
	<form name = "myForm" action = "">

		<p> 
			Enter Sales Amount: <br />
			<input name = "input" type = "text" />
			<input name = "Submit" type = "button" value =
				"Submit" onclick = "calculate(myForm.input.value)" /><br />
		</p>
		<p>
			<textarea name = "result" rows = "11" cols = "60">
			</textarea>
		</p>
	</form>
</body>
</html>


This is my current code. When i replace my arrays with variables the webpage works. Did i do something wrong with my arrays?
Was This Post Helpful? 0
  • +
  • -

#5 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Salary

Posted 17 April 2008 - 03:47 PM

you have a typo in the output function
you typed rangeOfSales and it should be rangeOfsales
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1