Jump to content

I need some help optimizing this script.


hotdog1983
 Share

Recommended Posts

Hi guys, I'm a noob in javascript.. I'm trying to optimize my site.I have a code like this that rounds up numbers to different decimal points depending on the input.

function getXMLHTTP() { 		var xmlhttp=false;			try{			xmlhttp=new XMLHttpRequest();		}		catch(e)	{					try{							xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");			}			catch(e){				try{				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");				}				catch(e1){					xmlhttp=false;				}			}		}		 			return xmlhttp;	}function getCurrencyCode(strURL){  var req = getXMLHTTP();  if (req)  {	//function to be called when state is changed	req.onreadystatechange = function()	{		//when state is completed i.e 4	  if (req.readyState == 4)	  {		// only if http status is "OK"		if (req.status == 200)		{			exr = req.responseText.split("|");			if (isNaN(exr[0]))			{			alert("There was a problem retrieving exchange rate. Please try again.");			return false;			}			if (exr[1]=="")			{			document.getElementById('usf').innerHTML=(exr[0]*1.3).toFixed(3);			document.getElementById('usm').innerHTML=(exr[0]*1.3).toFixed(3);			document.getElementById('ukf').innerHTML=(exr[0]*1.0).toFixed(3);			document.getElementById('ukm').innerHTML=(exr[0]*14.14).toFixed(3);			document.getElementById('chf').innerHTML=(exr[0]*1.63).toFixed(3);			document.getElementById('chm').innerHTML=(exr[0]*1.63).toFixed(3);			document.getElementById('thf').innerHTML=(exr[0]*1.26).toFixed(3);			document.getElementById('thm').innerHTML=(exr[0]*1.26).toFixed(3);			document.getElementById('caf').innerHTML=(exr[0]*0.68).toFixed(3);			document.getElementById('cam').innerHTML=(exr[0]*0.68).toFixed(3);			document.getElementById('skf').innerHTML=(exr[0]*5.5).toFixed(3);			document.getElementById('skm').innerHTML=(exr[0]*5.7).toFixed(3);			document.getElementById('auf').innerHTML=(exr[0]*1.6).toFixed(3);			document.getElementById('aum').innerHTML=(exr[0]*10.28).toFixed(3);			document.getElementById('inf').innerHTML=(exr[0]*2.76).toFixed(3);			document.getElementById('inm').innerHTML=(exr[0]*3.94).toFixed(3);			document.getElementById('spf').innerHTML=(exr[0]*1.64).toFixed(3);			document.getElementById('spm').innerHTML=(exr[0]*17.54).toFixed(3);			document.getElementById('brf').innerHTML=(exr[0]*2.11).toFixed(3);			document.getElementById('brm').innerHTML=(exr[0]*17.29).toFixed(3);			document.getElementById('jaf').innerHTML=(exr[0]*3.25).toFixed(3);			document.getElementById('jam').innerHTML=(exr[0]*13.68).toFixed(3);			document.getElementById('gef').innerHTML=(exr[0]*1.86).toFixed(3);			document.getElementById('gem').innerHTML=(exr[0]*16.69).toFixed(3);			document.getElementById('frf').innerHTML=(exr[0]*1.87).toFixed(3);			document.getElementById('frm').innerHTML=(exr[0]*14.44).toFixed(3);			document.getElementById('nzf').innerHTML=(exr[0]*2.07).toFixed(3);			document.getElementById('nzm').innerHTML=(exr[0]*23.08).toFixed(3);			document.getElementById('coin').innerHTML=exr[2];			}			else if (exr[1]=="nodec")			{			document.getElementById('usf').innerHTML=(exr[0]*1.3).toFixed(1);			document.getElementById('usm').innerHTML=(exr[0]*1.3).toFixed(1);			document.getElementById('ukf').innerHTML=(exr[0]*1.0).toFixed(1);			document.getElementById('ukm').innerHTML=(exr[0]*14.14).toFixed(1);			document.getElementById('chf').innerHTML=(exr[0]*1.63).toFixed(1);			document.getElementById('chm').innerHTML=(exr[0]*1.63).toFixed(1);			document.getElementById('thf').innerHTML=(exr[0]*1.26).toFixed(1);			document.getElementById('thm').innerHTML=(exr[0]*1.26).toFixed(1);			document.getElementById('caf').innerHTML=(exr[0]*0.68).toFixed(1);			document.getElementById('cam').innerHTML=(exr[0]*0.68).toFixed(1);			document.getElementById('skf').innerHTML=(exr[0]*5.5).toFixed(1);			document.getElementById('skm').innerHTML=(exr[0]*5.7).toFixed(1);			document.getElementById('auf').innerHTML=(exr[0]*1.6).toFixed(1);			document.getElementById('aum').innerHTML=(exr[0]*10.28).toFixed(1);			document.getElementById('inf').innerHTML=(exr[0]*2.76).toFixed(1);			document.getElementById('inm').innerHTML=(exr[0]*3.94).toFixed(1);			document.getElementById('spf').innerHTML=(exr[0]*1.64).toFixed(1);			document.getElementById('spm').innerHTML=(exr[0]*17.54).toFixed(1);			document.getElementById('brf').innerHTML=(exr[0]*2.11).toFixed(1);			document.getElementById('brm').innerHTML=(exr[0]*17.29).toFixed(1);			document.getElementById('jaf').innerHTML=(exr[0]*3.25).toFixed(1);			document.getElementById('jam').innerHTML=(exr[0]*13.68).toFixed(1);			document.getElementById('gef').innerHTML=(exr[0]*1.86).toFixed(1);			document.getElementById('gem').innerHTML=(exr[0]*16.69).toFixed(1);			document.getElementById('frf').innerHTML=(exr[0]*1.87).toFixed(1);			document.getElementById('frm').innerHTML=(exr[0]*14.44).toFixed(1);			document.getElementById('nzf').innerHTML=(exr[0]*2.07).toFixed(1);			document.getElementById('nzm').innerHTML=(exr[0]*23.08).toFixed(1);			document.getElementById('coin').innerHTML=exr[2];			}			else			{			document.getElementById('usf').innerHTML=(exr[0]*1.3).toFixed(2)+exr[1];			document.getElementById('usm').innerHTML=(exr[0]*1.3).toFixed(2)+exr[1];			document.getElementById('ukf').innerHTML=(exr[0]*1.0).toFixed(2)+exr[1];			document.getElementById('ukm').innerHTML=(exr[0]*14.14).toFixed(2)+exr[1];			document.getElementById('chf').innerHTML=(exr[0]*1.63).toFixed(2)+exr[1];			document.getElementById('chm').innerHTML=(exr[0]*1.63).toFixed(2)+exr[1];			document.getElementById('thf').innerHTML=(exr[0]*1.26).toFixed(2)+exr[1];			document.getElementById('thm').innerHTML=(exr[0]*1.26).toFixed(2)+exr[1];			document.getElementById('caf').innerHTML=(exr[0]*0.68).toFixed(2)+exr[1];			document.getElementById('cam').innerHTML=(exr[0]*0.68).toFixed(2)+exr[1];			document.getElementById('skf').innerHTML=(exr[0]*5.5).toFixed(2)+exr[1];			document.getElementById('skm').innerHTML=(exr[0]*5.7).toFixed(2)+exr[1];			document.getElementById('auf').innerHTML=(exr[0]*1.6).toFixed(2)+exr[1];			document.getElementById('aum').innerHTML=(exr[0]*10.28).toFixed(2)+exr[1];			document.getElementById('inf').innerHTML=(exr[0]*2.76).toFixed(2)+exr[1];			document.getElementById('inm').innerHTML=(exr[0]*3.94).toFixed(2)+exr[1];			document.getElementById('spf').innerHTML=(exr[0]*1.64).toFixed(2)+exr[1];			document.getElementById('spm').innerHTML=(exr[0]*17.54).toFixed(2)+exr[1];			document.getElementById('brf').innerHTML=(exr[0]*2.11).toFixed(2)+exr[1];			document.getElementById('brm').innerHTML=(exr[0]*17.29).toFixed(2)+exr[1];			document.getElementById('jaf').innerHTML=(exr[0]*3.25).toFixed(2)+exr[1];			document.getElementById('jam').innerHTML=(exr[0]*13.68).toFixed(2)+exr[1];			document.getElementById('gef').innerHTML=(exr[0]*1.86).toFixed(2)+exr[1];			document.getElementById('gem').innerHTML=(exr[0]*16.69).toFixed(2)+exr[1];			document.getElementById('frf').innerHTML=(exr[0]*1.87).toFixed(2)+exr[1];			document.getElementById('frm').innerHTML=(exr[0]*14.44).toFixed(2)+exr[1];			document.getElementById('nzf').innerHTML=(exr[0]*2.07).toFixed(2)+exr[1];			document.getElementById('nzm').innerHTML=(exr[0]*23.08).toFixed(2)+exr[1];			document.getElementById('coin').innerHTML=exr[2];			}		}		else		{			alert("There was a problem retrieving exchange rate. Please try again.");		}	  }		}	req.open("GET", strURL, true);	req.send(null);  }}</script>

As you can see, it's repetitive. I thought I could optimize to something like this.

			if (exr[1]=="")			{			var round = new toFixed(3);			}			else if (exr[1]=="nodec")			{			var round = new toFixed(1);			}			else			{			var round = new toFixed(2)+exr[1];			}			document.getElementById('usf').innerHTML=(exr[0]*1.3).round;			document.getElementById('usm').innerHTML=(exr[0]*1.3).round;			document.getElementById('ukf').innerHTML=(exr[0]*1.0).round;			document.getElementById('ukm').innerHTML=(exr[0]*14.14).round;			document.getElementById('chf').innerHTML=(exr[0]*1.63).round;			document.getElementById('chm').innerHTML=(exr[0]*1.63).round;			document.getElementById('thf').innerHTML=(exr[0]*1.26).round;			document.getElementById('thm').innerHTML=(exr[0]*1.26).round;			document.getElementById('caf').innerHTML=(exr[0]*0.68).round;			document.getElementById('cam').innerHTML=(exr[0]*0.68).round;			document.getElementById('skf').innerHTML=(exr[0]*5.5).round;			document.getElementById('skm').innerHTML=(exr[0]*5.7).round;			document.getElementById('auf').innerHTML=(exr[0]*1.6).round;			document.getElementById('aum').innerHTML=(exr[0]*10.28).round;			document.getElementById('inf').innerHTML=(exr[0]*2.76).round;			document.getElementById('inm').innerHTML=(exr[0]*3.94).round;			document.getElementById('spf').innerHTML=(exr[0]*1.64).round;			document.getElementById('spm').innerHTML=(exr[0]*17.54).round;			document.getElementById('brf').innerHTML=(exr[0]*2.11).round;			document.getElementById('brm').innerHTML=(exr[0]*17.29).round;			document.getElementById('jaf').innerHTML=(exr[0]*3.25).round;			document.getElementById('jam').innerHTML=(exr[0]*13.68).round;			document.getElementById('gef').innerHTML=(exr[0]*1.86).round;			document.getElementById('gem').innerHTML=(exr[0]*16.69).round;			document.getElementById('frf').innerHTML=(exr[0]*1.87).round;			document.getElementById('frm').innerHTML=(exr[0]*14.44).round;			document.getElementById('nzf').innerHTML=(exr[0]*2.07).round;			document.getElementById('nzm').innerHTML=(exr[0]*23.08).round;			document.getElementById('coin').innerHTML=exr[2];		}

But this doesn't work and I think it's not a right coding in Javascript.Could anyone give me some hints?Thank you.

Link to comment
Share on other sites

Try this. Create an object something like this:

var rates = new Object();rates['usf'] = 1.3;rates['usm'] = 1.3;rates['ukf'] = 1.0;// on and on till you assign all the values.

The reason for doing that is to improve access to the data. Your way, we have to hard-code it over and over. This way, we code it once and forget about it. An object structure also gives us something we can loop through, and loops are one of the first things to look for when optimizing code.Now, let's use your original conditionals to set some values

var myDecimal;var myIncrement = 0;if (exr[1] == "") {   myDecimal = 3;}else if (exr[1] == "nodec") {   myDecimal = 1;}else {   myDecimal = 2;   myIncrement = exr[1];}

Finally, we'll loop through this thing. But notice how we replace hard-coded values with variables, and how we only need to loop once:

for (var i in rates) {   document.getElementById(i).innerHTML = (exr[0] * rates[i]).toFixed(myDecimal) + myIncrement;}document.getElementById('coin').innerHTML = exr[2]; // it's the same always, right?

I haven't actually run this, but it should be pretty close. Maybe I left out something. I don't know what exr[1] actually is, so I don't know if it needs to be cast or not. The point is, don't copy/paste and hope it works off the shelf. :)

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...