Record 18: Converting between Units of Measurements function convertTemperature(fMeasure, tMeasure, val) { fMeasure = fMeasure.toLowerCase( ); tMeasure = tMeasure.toLowerCase( ); if ( (fMeasure == "centigrade" || fMeasure == "celsius" || fMeasure == "c") && (tMeasure == "fahrenheit" || tMeasure == "f") ) { return (val * 9/5) + 32; } else if ( (fMeasure == "fahrenheit" || fMeasure == "f") && (tMeasure == "centigrade" || tMeasure == "celsius" || tMeasure == "c") ) { return (val - 32) * 5/9; } else { trace ("Invalid conversion type from " + fMeasure + " to " + tMeasure); return NaN; } }trace("----------------------------------------------------------------------"); trace("converting 32 from centigrade to fahrenheit:"+convertTemperature("c","f",32)); trace("converting 64 from fahrenheit to centigrade:"+convertTemperature("f","c",64)); trace("----------------------------------------------------------------------"); function convertToCentigrade(fMeasure, val) { fMeasure = fMeasure.toLowerCase( ); if (fMeasure == "kelvin" || fMeasure == "k") { return (val - 273.15); } else if ( fMeasure == "fahrenheit" || fMeasure == "f" ) { return (val - 32) * 5/9; } else if (fMeasure == "centigrade" || fMeasure == "celsius" || fMeasure == "c") { return val; } else { return NaN; } } trace("----------------------------------------------------------------------"); trace("converting 78 from kelvin to centigrade:"+convertToCentigrade("k",78)); trace("converting 38 from fahrenheit to centigrade:"+convertToCentigrade("f",38)); trace("displaying the celsius value:"+convertToCentigrade("c",60)); trace("----------------------------------------------------------------------"); function convertFromCentigrade(tMeasure, val) { tMeasure = tMeasure.toLowerCase( );
if (tMeasure == "kelvin" || tMeasure == "k") { return (val + 273.15); } else if ( tMeasure == "fahrenheit" || tMeasure == "f" ) { return (val * 9/5) + 32; } else if (tMeasure == "centigrade" || tMeasure == "celsius" || tMeasure == "c") { return val; } else { return NaN; } } trace("----------------------------------------------------------------------"); trace("converting centigrade value 42 to kelvin:"+convertFromCentigrade("k",42)); trace("converting centigrade value 56 to farhenheit:"+convertFromCentigrade("f",56)); trace("displaying celsius value:"+convertFromCentigrade("c",72)); trace("----------------------------------------------------------------------"); /*function convertTemperature(fMeasure, tMeasure, val) { var centigradeVal = Math.convertToCentigrade (fMeasure, val); return Math.convertFromCentigrade (tMeasure, centigradeVal ); }*/ function convertWeights(fMeasure, tMeasure, val) { if (fMeasure == "pounds" && tMeasure == "kilograms") { return val / 2.2; } else if (fMeasure == "kilograms" && tMeasure == "pounds") { return val * 2.2; } else { return "invalid conversion type"; } } trace("----------------------------------------------------------------------"); trace("converting 98 from pounds to kilograms:"+convertWeights("pounds","kilograms",98)); trace("converting 89 from kilograms to pounds:"+convertWeights("kilograms","pounds",89)); trace("----------------------------------------------------------------------");
-----------------------------------------------------------------------------------output ---------------------------------------------------------------------converting 32 from centigrade to fahrenheit:89.6 converting 64 from fahrenheit to centigrade:17.7777777777778
------------------------------------------------------------------------------------------------------------------------------------------converting 78 from kelvin to centigrade:-195.15 converting 38 from fahrenheit to centigrade:3.33333333333333 displaying the celsius value:60 ------------------------------------------------------------------------------------------------------------------------------------------converting centigrade value 42 to kelvin:315.15 converting centigrade value 56 to farhenheit:132.8 displaying celsius value:72 ------------------------------------------------------------------------------------------------------------------------------------------converting 98 from pounds to kilograms:44.5454545454545 converting 89 from kilograms to pounds:195.8 ----------------------------------------------------------------------