Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Can't figure out what's wrong. Help with basic math calculations.

    I am making a simple javascript where I ask for three number inputs from the user. The script then returns sum, product, average, largest, and smallest.

    The largest and smallest numbers are coming out, but not the arithmetic.

    Hoping someone can tell me what is wrong.

    Code:
    	<script type = "text/javascript">
    	
    	<!--
    		var input_one;
    		var input_two;
    		var input_three;
    		var firstNumber;
    		var secondNumber;
    		var thirdNumber;
    		var largest;
    		var smallest;
    
    		input_one = window.prompt( "Please Type a Number" );
    		input_one = window.prompt( "And Another One" );
    		input_three = window.prompt( "Just One More" );
    		
    		 firstNumber = parseInt (input_one );
    		 secondNumber = parseInt ( input_two );
    		 thirdNumber = parseInt ( input_three );
    		
    		largest = firstNumber;	
    		smallest = firstNumber;
    		
    		if (secondNumber > largest)
    		largest = secondNumber;
    			
    		if (thirdNumber > largest)
    		largest = thirdNumber;
    		
    		if (secondNumber < smallest)
    		smallest = secondNumber;
    			
    		if (thirdNumber < smallest)
    		smallest = thirdNumber;
    			
    		 window.alert(
    		 	"Sum: " + ( firstNumber + secondNumber + thirdNumber ) + "\n" + 
    		 	"Average: " + ( ( firstNumber + secondNumber + thirdNumber ) / 3 ) + "\n" +
    		 	"Product: " + ( firstNumber * secondNumber * thirdNumber) + "\n" + 
    		 	"Largest: " + largest + "\n" + 
    		 	"Smallest: " + smallest );
    		//-->
    		
    	</script> 
    			
    </head>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    input_one = window.prompt( "Please Type a Number" );
    input_one = window.prompt( "And Another One" );
    I don't know if you can blame this one on arithmetic

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Code:
    input_one = window.prompt( "Please Type a Number" );
    input_one = window.prompt( "And Another One" );
    input_three = window.prompt( "Just One More" );
    Took a few minutes to spot this Edit: xelawho got there before me..
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    Although legal is is a bad idea to omit the braces around a one-line if statement. It is recommended that you place the opening brace following the function, if, else, for, while, do, switch, and try statements on the same line and not on the following line. Apart from that every Javascript statement should be followed by a semi-colon. It is quite possible to disregard this advice, but if you do one day it will rise up and bite you in the undercarriage.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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