Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript Problem ;/

Options
  • 26-04-2005 12:38pm
    #1
    Registered Users Posts: 3,945 ✭✭✭


    Hey,

    I'm writing a little script for a temperature converter. So far I have:
    		var limit;
    		
    		function maximum( ){
    			return Math.max( w, Math.max( x, Math.max( y, z )));
    		}
    
    		function celsius( input_temp )
    		{
    		  var c = 5.0/9.0*(input_temp-32);
    			document.write( "<b><u>Temperature in Celsius:</u></b> " + c + "<br>" );
    		}
    		
    		function farenheit( input_temp )
    		{
    		  var f = 9.0/5.0*input_temp+32;
    			document.write( "<b><u>Temperature in Farenheit:</u></b> " + f + "<br>" );
    		}
    		
    		function output()
    		{
    		 limit = window.prompt( "How many temperatures would you like to convert?", "1" );
    		 for( var loop = 1 ; loop <= limit ; loop++ )
    		  {
    		 			 var input_temp = window.prompt( "Enter the temperature you wish to convert.", "0" );
    					 var scale = window.prompt( "Which scale would you like to convert to? 'Celsius = 0' 'Farenheit = 1'", "0" );
    					 input_temp = parseFloat( input_temp );
    						
    					 if( scale == 0 ){
    						  celsius( input_temp );
    						}
    					 else{
    						  farenheit( input_temp );
    			}
    		}
    		}
    		
    		document.write( "<h1>Temperature Converter</h1>" );
    		document.write( "---------------------------" + "<br>" + "<p>" );
    		
    		output();
    		
    		document.write( "<br>" + "<p>" + "---------------------------" + "<br>" + "<p>" );
    		
    		maximum( );
    		
    		document.write( "<br>" + "<p>" + "---------------------------" + "<br>" + "<p>" );
    
    
    
    

    All that the script has to do is accept a few temperatures through the "window.prompt" and then convert to whatever scale the user selects. The final temperature is outputted for each one but I have to also show which is the highest value. This is where I'm stuck. The only problem is I don't know how I can assign each converted temperature to a variable so that I can put them into the function, "maximum()".

    Is this even the right way to go about it? If someone could point me in the right direction, that would be great :).


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    For methods/functions, and this isn't just for OOP, they should never, ever output or write data, unless the primary function of the function/method is output.

    That is, in this example, the primary purpose of the celsius and farenheit functions is to convert the temperature. So remove the document.write from both, and instead just return the value. Then you can use this data as you want, as many times as you want.


  • Registered Users Posts: 365 ✭✭jayo99


    function maximum( x, y){
    return Math.max( x, y);
    }

    function celsius( input_temp )
    {
    var c = 5.0/9.0*(input_temp-32);
    document.write( "<b><u>Temperature in Celsius:</u></b> " + c + "<br>" );
    return c;
    }

    function farenheit( input_temp )
    {
    var f = 9.0/5.0*input_temp+32;
    document.write( "<b><u>Temperature in Farenheit:</u></b> " + f + "<br>" );
    return f;
    }

    function output()
    {
    var maximumTemp = -999;
    limit = window.prompt( "How many temperatures would you like to convert?", "1" );
    for( var loop = 1 ; loop <= limit ; loop++ )
    {
    var input_temp = window.prompt( "Enter the temperature you wish to convert.", "0" );
    var scale = window.prompt( "Which scale would you like to convert to? 'Celsius = 0' 'Farenheit = 1'", "0" );
    var curTemp = 0;
    input_temp = parseFloat( input_temp );
    if( scale == 0 ){
    curTemp = celsius( input_temp );
    }
    else{
    curTemp = farenheit( input_temp );
    }

    maximumTemp = maximum (maximumTemp, curTemp);

    }
    return maximumTemp;
    }

    document.write( "<h1>Temperature Converter</h1>" );
    document.write( "
    " + "<br>" + "<p>" );
    var mx = output();
    document.write( "<br>" + "<p>" + "
    " + "<br>" + "<p>" );
    document.write( "<br>" + "<p>" + " Maximum -> " + mx + "<p>" );
    document.write( "<br>" + "<p>" + "
    " + "<br>" + "<p>" );


  • Registered Users Posts: 3,945 ✭✭✭Anima


    Thanks lads :)

    I'm not entirely sure whats going on there Jayo, could you explain what you've done? Cheers


  • Registered Users Posts: 365 ✭✭jayo99


    yeah...

    I changed both the celcius & farenheit functions to return the value they calculate..

    I also added a new variable "maximumTemp" in the function output.. I initialise it with -999, so that the first value when compared to it will be greater..

    The for loop iterates n times working out the maximum value each time and at the end prints out "maximumTemp"


Advertisement