Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript Newbie

Status
Not open for further replies.

fieldsy36

Technical User
Apr 6, 2011
1
AU
I have written the following JavaScript, but none of the code seems to work at all. I am at a loss as to what the errors are, Firefox's debugger did not help, and I don't know what to do. Can anyone help?

Code:
<html>
	<head>
		<title>Solar Energy Products</title>
		<script language="Javascript">
		
       function main (txtPamphletMin,txtPamphletMax,optQuality,selIncrement,txtResults)
            {
		
				var pamphletMin,pamphletMax,quality,increment;
				var amount = 100;
				var errorMsg = "";
				var results = "";
				pamphletMin = parseInt(pamphletMin.value); //Make sure that the minimum number of pamphlets required is entered


				if (pamphletMin < 100)  
				{
				     errorMsg = "Number of pamphlets must be above 100";
				}
				else
                {
					pamphletMin = txtPamphletMin.value; //get the number of pamphlets from the text box
				}
				
				if(errorMsg == "")  //still no errors
				{
					quality = getQuality(optQuality);
					if(quality == "")  //this means that no processor was selected
					{
						errorMsg = "Quality type must be selected";
					}
				}
				
				if(errorMsg == "")  //still no errors
				{
					increment = getIncrement(selIncrement);
					if(increment < 0)
					{
						errorMsg = "Increment amount must be selected";
					}
				}
				
				if(errorMsg == "")
				{
				    txtResults.value = generatePriceTable (pamphletMin,pamphletMax,quality,increment)
                }
                else
                {
                    txtResults.value = errorMsg;
				}
                                
            }function  validatePamphletMin(pamphletMin)
            {
                var errorMsg = "";
                if(pamphletMin == "")
                {
                    errorMsg = "Please enter Minimum amount of pamphlets";
                }
                else
                {
                
				}


            function getQuality(optQuality)
            {
                var selNdx = -1, selValue, quality;
				
                for(var i = 0; i < optQuality.length; i++)
                {
                    if(optQuality[i].checked == true)
                   	{
                        selNdx = i; // grab index
                        break;      // bail out of loop
                   	}
                }
                if(selNdx == -1)
                {
                    quality = "";
                }
                else
                {
                    quality = optQuality[selNdx].value;
                } 
				return quality;
            }
      
			function getPamphletIncrement(selPamphlet)
			{
				var pamphletIncrement, selValue;
				
                var selNdx = selIncrement.selectedIndex;
                if (selNdx == -1)   //this means nothing has been selected
				{
					pamphletIncrement = -1;
				}
				else
				{
					selValue = selPamphletIncrement.options[selNdx].value;                
					pamphletIncrement = parseInt(selValue);
				}
				
				return pamhpletIncrement;
			
			}
	  
		function generatePriceTable (pamphletMin,pamphletMax,quality,increment)
 
		{
		     var count, unitPrice, results, discount, discAmt, totalPrice, finalPrice;
		     results = "Hello " + name + 					 
					"\nYou want to order the following :- \n" +
					"Quality of Pamphlets : " + quaility + "\n" +
					"          Amount of pamphlets              Price\n";
					
		    unitPrice = calcPrice(quality, increment); 

			count = 1;
			while (count <= 20)
			{
				if (count < 6)
				{
					discount  = 0;
				}
				else
				{
					if (count < 11)
					{
						discount = 5;
					}
					else
					{
						if (count < 16)
						{
							discount = 10;
						}
						else
						{
							discount = 15;
						}
					}
				}

				totalPrice = count * unitPrice;
				finalPrice = totalPrice - (totalPrice * discount / 100);
				results = results + "             " + fmtAmt(count,0,2) + "                   " + fmtAmt(finalPrice,2,8) + "\n";
				count = count + 1;
			}
            return results;
		}		
	  
		function calcPrice(quality, increment)
		{
			
			var price = 0;
			if(pquality == "A")
			{
			     price = 600;
			}
			else
			{
				price = 700;
			}
				 
			price = price + quality * 0.225
				
			return price;
				
		}
		function fmtAmt(amt, numDp, width)
		{
			/* Returns string version of amt rounded to numDp decimal places, with
				numDp digits after decimal, in a field width wide, right aligned */
			var strAmt;
			if(numDp > 0)
			{
				strAmt = "" + Math.round(amt * Math.pow(10, numDp)) / Math.pow(10, numDp);
			}
			else
			{
				strAmt = "" + Math.round(amt);
			}
			if(numDp > 0)
			{
				if(strAmt.indexOf(".") == -1) strAmt += ".";
				while(strAmt.length - strAmt.indexOf(".") < numDp + 1)
				strAmt += "0";
			}
				while(strAmt.length < width)
			{
				strAmt = " " + strAmt;
			}
			return strAmt;
		}            
        </script>
    </head>
    <body onLoad="">
        <h1>Advertising Cost Calculation</h1>
        <form name="frmBuild">
            <b>Minimum number of Pamphlets required </b><input type="Text" name="txtName" value="100">
	     &nbsp;&nbsp;&nbsp;&nbsp; <b>Maximum Number of Pamphlets required</b> <input type="Text" name="txtEmail" value="600">	
            <br><b>Quality</b> &nbsp;&nbsp;
			<input type="Radio" name="optType" value="A" >A
			<input type="Radio" name="optType" value="B">B
			<input type="Radio" name="optType" value="C">C
			<input type="Radio" name="optType" value="D">D
             &nbsp;&nbsp; <b>Increment in Number of Pamphlets required: </b>
            <select name="selHardDrive" size="1" >
				<option value="100" >100 </option>
				<option value="200">200</option>
				<option value="500">500</option>
			</select>
			
			
            <p><input type="Button" value="Calculate Advertising Costs" onClick="main(txtName,txtEmail,optType,selHardDrive,txtResults);">
            <p><textarea name="txtResults" rows="27" cols="70"></textarea>
        </form>
    </body>
</html>
 
Hi

fieldsy36 said:
Firefox's debugger did not help
No kidding.
Code:
[b]Error: missing } after function body[/b]
[u]file:///master/fieldsy36.html[/u][right]Line: 195[/right]
Code:
[gray]09:24:10.563[/gray] [highlight orange] [/highlight] [COLOR=orange][b]x[/b][/color] missing } after function body[right][blue][u]fieldsy36.html:195[/u][/blue][/right]
Code:
[COLOR=white red] X [/color] [red]missing } after function body[/red][right][b][blue]fieldsy36.html (line 195)[/blue][/b][/right]
The [tt]function[/tt] who's declaration misses the closing brace ( } ) is validatePamphletMin().

Please indent your code consistently. Is easier to read and debug it when it is formatted consistently.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top