...

View Full Version : Not sure why NOTHING is happening



babylikesburgh
08-06-2008, 03:23 PM
I've gotten to this point with my code. It should input sales, calculate commission and total pay and add 1 to each array (which represents the ranges of income) to finally show the total number of each employee earning sales in each range. Yet, nada is happening. Any suggestions?


<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Calling a function</title>
<script type = "text/javascript">
<!--

var empSales
var empSalesInt
var basePay = 200
var total = new Array(9)

start();

function start()
{
for ( var i = 0; i < a.length; ++i )
total[i]=0
}


function addToArray()
{

var x;
var sales = document.getElementById( "inputVal" ).value;
var salary;

salary = 200 + parseInt(sales.newEntry.value ) * 0.09;
x = Math.floor( salary / 100 );

if( salary < 0 )
return
else if( x > 9 )
{
x = 10
++total[x-2]
}
if( x > 8 )
{
x = 9
++total[x-2]
}
else if( x > 7 )
{
x = 8
++total[x-2]
}
if( x > 6 )
{
x = 7
++total[x-2]
}
else if( x > 5 )
{
x = 6
++total[x-2]
}
if( x > 4 )
{
x = 5
++total[x-2]
}
else if( x > 3 )
{
x = 4
++total[x-2]
}
if( x > 2 )
{
x = 3
++total[x-2]
}
else if( x > 1 )
{
x = 2
++total[x-2]
}
outputArray();
}



function outputArray()
{
var sales = document.getElementById( "inputVal" );
document.write( "<h3>The total sales by amount ranges are: " +
"$200-299: " + total[0] +
"$300-399: " + total[1] +
"$400-499: " + total[2] +
"$500-599: " + total[3] +
"$600-699: " + total[4] +
"$700-799: " + total[5] +
"$800-899: " + total[6] +
"$900-999: " + total[7] +
"$1000 and over: " + total[8] + "</h3>" );
}

//-->
</script>

<body>
<form action = "">
<p>Enter sales for current employee for this week:<br />
<input id = "inputVal" type = "text" />
<input type = "button" value = "Enter"
onclick = "addToArray()" /><br /></p>

</form>
</body>
</html>

Kor
08-06-2008, 03:32 PM
At a first glance:


for ( var i = 0; i < a.length; ++i )

There is no variable a defined. Nor I could guess what was in your mind when writing it.

Kor
08-06-2008, 03:49 PM
However, when you write embedded script codes under an XHTML Doctype, you must isolate the code inside CDATA block islands, to prevent an XML markup interpretation of some special characters as < > &


<script type="text/javascript">
/*<![CDATA[*/
... code here ...
/*]]>*/
</script>

babylikesburgh
08-06-2008, 04:08 PM
Kor,

I changed the a.length to total.length, I had forgotten I had changed the name of my array =) BUT, still nothing is happening. I type in a number in my form inputVal, but after clicking the button, nothing... And I haven't used the CDATA blocks before with any of my JS codes and they all have worked fine (seems like I lost part of my brain this week ^_^), I tried that though, and still nothing. When I click the button, the IE status bar says DONE, but with errors. I'm about two incorrect tries away from bawling myself into oblivion. I HATE when I can't get my code to work, especially when it LOOKS so right to me... :(

Kor
08-06-2008, 05:43 PM
I haven't used the CDATA blocks before with any of my JS codes and they all have worked fine
No way if the code uses > < or &, under an XHTML Doctype, and especially on Internet Explorer.

However, can you describe what your calculation should do and put your problem in a linear algebraical expression? With some examples, please? We might rewrite the code.

babylikesburgh
08-06-2008, 06:28 PM
Ok, my code is basically still the same as above, but this is the problem:


A company pays its salespeople on a commission basis. The salespeople receive $200 per week, plus 9&#37; of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, which is a total of $650. Write a script (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges:

a. $200-299
b. $300-399
c. $400-499
d. $500-599
e. $600-699
f. $700-799
g. $800-899
h. $900-999
i. $1000 and over

I know I have the general concept right, but I've got two other scripts I'm working on that are pretty tough, so I'm seeking some advice on what to change. I'm not so positive on my if-else statements. Eh...

Kor
08-06-2008, 08:11 PM
Could be something like this (with a reset function and a number validator as bonus)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript">
/*<![CDATA[*/
var salesmen={};//the salesmen object
for(var i=200;i<=1100;i+=100){
salesmen[i]=0;
}
function addToArray(inp){
if(isNaN(inp.value)){inp.value='';return}//number validation
var salary=200+inp.value*9/100;
var spans=document.getElementById('container').getElementsByTagName('span'), j=0;
for (a in salesmen){
if(salary<Number(a)&&Number(a)!=1100){
salesmen[a]++;
spans[j-1].innerHTML=salesmen[a];
break
}
j++
}
if(salary>=1000){
salesmen[1100]++;
spans[8].innerHTML=salesmen[1100];
}
}
function resetValues(){
for(a in salesmen){
salesmen[a]=0;
}
var spans=document.getElementById('container').getElementsByTagName('span'), j=0, s;
while(s=spans[j++]){
s.innerHTML=0;
}
}
/*]]>*/
</script>
</head>
<body>
<div id="container">
<div>$200-299 <span>0</span></div>
<div>$300-399 <span>0</span></div>
<div>$400-499 <span>0</span></div>
<div>$500-599 <span>0</span></div>
<div>$600-699 <span>0</span></div>
<div>$700-799 <span>0</span></div>
<div>$800-899 <span>0</span></div>
<div>$900-999 <span>0</span></div>
<div>$1000 and over <span>0</span></div>
</div>
<br />
<form action="">
<input type="text" name="inputVal" />
<input type="button" value="Enter" onclick="addToArray(inputVal)" />
<input type="button" value="Reset" onclick="resetValues()" />
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum