SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy So many errors in my script:( pls help

    Hi
    can one of you javascript experts please help me

    I am new to Javascript and have written this script but it is full of errors.
    Can someone pleas point them out to me. It would be greatly appreciated
    Thank you.
    Now here is my script:
    Code:
    <HTML>
    <HEAD>
    <TITLE> </TITLE>
    
    <SCRIPT LANGAUGE = "JavaScript">
    
    	function begin();
    	{
          		// Initialize calculation type array
    	       	var calcType = new Array(5);
    
    	 		for( var arrayCtr = 0; arrayCtr < calcType.length; arrayCtr++ )
    			{
    				if(arrayCtr = 0)
    					calcType[arrayCtr] = "addition";
    				else
    					if(arrayCtr == 1)
    					    calcType[arrayCtr] = "subtraction";
    				else
    					if(arrayCtr == 2)
    						calcType[arrayCtr] = multiplication;
    				else
    					if(arrayCtr == 3)
    						calcType[arrayCtr] = "division";
    				else
    					if(arrayCtr == 4)
    						calcType[arrayCtr] = "modulus";
    			}
    
    		// prompt user for calculation type, with the five options specified on the second line displayed
    		do {
    			var type = window.prompt("Please select the desired operation type:<br>1-Addition, 2-Subtraction, 3-Multiplication, 4-Division, 5-Modulus", "0");
    		} while(type > 0 && type < 6);
    
    		/*
    		   prompt user for the first number, validate that the first character in the string argument is a number,
    		   parse the string from left to right until it reaches the end of the number, discard any characters that
    		   occur after the end of the number input, and finally return the number as a number (not as a string)
    
    		var num1 = parseInt(window.prompt("Please enter the first number: ", "0"));
    
    		// prompt user for second number, then perform the same conversion as the first number
    		var num2 = parseFloat(window.alert("Please enter the second number: ", "0"));
    
    		switch(type)
    		{
    
    			case 1:
    				// call addition function
    				var result = add(num1, num2);
    				break;
    
    			case 2:
    				// call subtraction function
    				var result = subtract(num 1, num2);
    				break;
    
    			case 3:
    				// call multiplication function
    				var result = multiply(num1, num2);
    				break;
    
    			case 4:
    				// call division function
    				var result = divide(num1, num2);
    				break;
    
    			case 5:
    				// call mod (remainder) function
    				var result = mod(num1, num2);
    				break;
    
    			default:
    				window.alert("Invalid operation type")
    
    
    	// display the results of the calculation in a window alert box
    	document.writeln("The result of " + calcType [type] + " is: " + result);
    
    	}
    
    	// addition function definition
    	function add(num1, num2){
    
    	   return num1 + num2;
    
    	}
    
    	// subtraction function definition
    	function subtract(num1, num2){
    
    	   return num1 - num2;
    
    	}
    	
    	// multiplication function definition
    	function multiply(num1, num 2){
    
    	   return num1 * num2;
     
    	}
    
    	// division function definition
    	function divide(num1, num2){
    
    	   return num1 / num2;
    
    	}
    
    	// modulus (remainder) function definition
    	function modulus(num1, num2){
    
    	   return num1 ^ num2;
    
    	}
    
    </SCRIPT>
    
    <BODY ONLOAD = "begin">
    <br>Press F1 or click the Refresh (or Reload) button to run this script again</br>
    </BODY>
    <HTML>
    Thank you so so much for any help you can give. thank you

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,141
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    bugs

    Hi heather lindsay, welcome to the forums,
    What error messages are you getting?

  3. #3
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing that is coming up on my page is
    :
    Press F1 or click the Refresh (or Reload) button to run this script again

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,141
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    Errors

    Although what is rendered can help debug a script, you said "full of errors" so I'm more interested error messages ATM.

    What browser are you using?

    In IE, clicking on the little yellow alert triangle in the status bar brings up an error dialog box. It indicates
    Line: 119
    Error: 'begin' is undefined

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,141
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    bugs

    So how you doing with the bugs? I did manage to find a few and get the page to "work" so far as the script is written.
    Parhaps the first thing to do is get the mark-up so it's at least well formed. ie. fix missing tags, end tag matches start tag etc.
    What browser are you using?

  6. #6
    SitePoint Member
    Join Date
    Sep 2007
    Location
    china beijing
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for( var arrayCtr = 0; arrayCtr < calcType.length; arrayCtr++ )
    {
    if(arrayCtr = 0)
    here :if(arrayCtr = 0)
    should it be if(arrayCtr == 0) ==0?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •