www.webdeveloper.com
Results 1 to 8 of 8

Thread: Some basic javascript question

  1. #1
    Join Date
    Dec 2010
    Posts
    4

    Some basic javascript question

    Hi guys, I'm a noob in javascript but 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.

    Code:
    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];
    
    			}
    As you can see, lots of them are repetitive... So i've tried to rewrite to something like this

    Code:
    			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 seem to be a right coding in javascript..
    Can I achieve optimizing this code?
    I have a feeling that this could be very easy to solve.

    Thanks a lot!

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Lightbulb

    This is untested (because I don't have all the code you are using) and incomplete.
    Add in the values for 'Rate2' and 'Rate3' similar to the template for 'Rate1'.
    Code:
    var IDS = ['usf','usm','ukf','ukm','chf','chm','thf','thm','caf','cam',
               'skf','skm','auf','aum','inf','inm','spf','spm','brf','brm',
               'jaf','jam','gef','fem','frf','frm','nzf','nzm'];
               
    var Rate1=[ 1.5,  1.3,  1.0,  14.14,1.63, 1.63, 1.26, 1.26, 0,68, 0.68,
                5.5,  5.7,  1.6,  10.28,2.76, 3.94, 1.64,17.54, 2.11, 17.29,
                2.35,13.68, 1.86, 16.69,1.87,14.44, 2.07,23.08];
                
    var Rate2=[ RATES NEED TO BE SUPPLIED FOR EACH IDS ABOVE ];
    
    var Rate3=[ RATES NEED TO BE SUPPLIED FOR EACH IDS ABOVE ];
               
    if (exr[1]=="") {
      for (var i=0; i<IDS.length; i++) {
        document.getElementById(IDS).innerHTML=(exr[0]*Rate1[i]).toFixed(3);
      }
    } else {
      if (exr[1]=="nodec") {
        for (var i=0; i<IDS.length; i++) {
          document.getElementById(IDS).innerHTML=(exr[0]*Rate2[i]).toFixed(1);
        }
      } else {
        for (var i=0; i<IDS.length; i++) {
          document.getElementById(IDS).innerHTML=(exr[0]*Rate3[i]).toFixed(2)+exr[1];
        }
      }  
    }
    document.getElementById('coin').innerHTML=exr[2];

  3. #3
    Join Date
    Dec 2010
    Posts
    4
    WOW!!! That's the real code. I can save a lot of KBs with that.
    Thank you so much!!! I understood how your code will work but I can't get it to work and the problem is I can't see what went wrong...
    My whole code now is

    Code:
    <script type="text/javascript">
    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("|");
    				var IDS = ['usf','usm','ukf','ukm','chf','chm','thf','thm','caf','cam',
    						   'skf','skm','auf','aum','inf','inm','spf','spm','brf','brm',
    						   'jaf','jam','gef','fem','frf','frm','nzf','nzm'];
    						   
    				var Rate1=[ 1.5,  1.3,  1.0,  14.14,1.63, 1.63, 1.26, 1.26, 0,68, 0.68,
    							5.5,  5.7,  1.6,  10.28,2.76, 3.94, 1.64,17.54, 2.11, 17.29,
    							2.35,13.68, 1.86, 16.69,1.87,14.44, 2.07,23.08];
    							
    				var Rate2=[ 1.5,  1.3,  1.0,  14.14,1.63, 1.63, 1.26, 1.26, 0,68, 0.68,
    							5.5,  5.7,  1.6,  10.28,2.76, 3.94, 1.64,17.54, 2.11, 17.29,
    							2.35,13.68, 1.86, 16.69,1.87,14.44, 2.07,23.08];
    
    				var Rate3=[ 1.5,  1.3,  1.0,  14.14,1.63, 1.63, 1.26, 1.26, 0,68, 0.68,
    							5.5,  5.7,  1.6,  10.28,2.76, 3.94, 1.64,17.54, 2.11, 17.29,
    							2.35,13.68, 1.86, 16.69,1.87,14.44, 2.07,23.08];
    						   
    				if (exr[1]=="") {
    				  for (var i=0; i<IDS.length; i++) {
    					document.getElementById(IDS).innerHTML=(exr[0]*Rate1[i]).toFixed(3);
    				  }
    				} else {
    				  if (exr[1]=="nodec") {
    					for (var i=0; i<IDS.length; i++) {
    					  document.getElementById(IDS).innerHTML=(exr[0]*Rate2[i]).toFixed(1);
    					}
    				  } else {
    					for (var i=0; i<IDS.length; i++) {
    					  document.getElementById(IDS).innerHTML=(exr[0]*Rate3[i]).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>
    It's hard to post all the code here because it gets a response from another php file and that file calls another file again.
    But anyway I will try find a problem and thank you again.
    I guess I can use only Rate1 if Rate2 and Rate3 are the same yeah?

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391
    Question:
    Why do you have the JS code in the middle of the AJAX request.
    It should be in the <HEAD> or loaded in as an external JS file.

    If the array values are the same (Rate1 == Rate2 == Rate3), then yes, you need use just one ones that are different.

  5. #5
    Join Date
    Dec 2010
    Posts
    4
    Would there be any problem if I do it this way?
    The thing I wanted was the rates change dynamically without reloading the page when a user changes the currency selection(dropdown menu). But I didn't have any knowledge of AJAX or JS, so I used this solution http://apptools.com/phptools/xml/currency/howto.php. But that's just a script that retrieves exchange rates, So I had to look at http://roshanbh.com.np/2008/09/chang...-ajax-php.html and combined the codes.

    So the my code works like this.
    I have
    Code:
    <select style="width:216px;" name="currency" onChange="getCurrencyCode('forex.php?currency='+this.value)">
    in my form and when a user changes the currency, it will trigger forex.php.

    Forex.php contains series of cases like this.
    Code:
    <?php
    //error_reporting(0);
    $path=pathinfo($_SERVER['PHP_SELF']);
    $path=$_SERVER['DOCUMENT_ROOT'].$path['dirname'];
    require_once("$path/currencyexchange_class.php");
    $cx=new currencyExchange();
    $cx->getData();
    $currency=$_REQUEST['currency'];
    
    switch($currency)
    {
    	case "AUD" :
    		if ($euro=$cx->Convert("USD","AUD",1))
    		$a="c";$c="Cents";
    		break;
    	case "BRL" :
    		if ($euro=$cx->Convert("USD","BRL",1))
    		$a="c";$c="Centavos";
    		break;
    As you can see, the real rate is retrieved from another file that gets XML from a bank. I spent days in doing this and it worked okay so far but I wanted to optimize this.
    Do you see any problem doing this way or it's okay as long as the script works?
    Thanks again for your help!

  6. #6
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Arrow

    Well, you're asking questions above my pay grade.
    As I said before, I don't know anything about PHP.

    If that works in with the AJAX code, good, but I would be suprised if it does.
    Again, I'll defer to someone more knowledgeable about your problem.

    Good Luck!

  7. #7
    Join Date
    Dec 2010
    Posts
    4
    Yea it actually worked surprisingly!!
    I still believe I can follow your example which seems really beautiful.
    Thanks a lot for you help! I will probably be able to fix this some time soon.
    Wish you a happy holiday season!

  8. #8
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,391

    Arrow

    Quote Originally Posted by hotdog1983 View Post
    Yea it actually worked surprisingly!!
    I still believe I can follow your example which seems really beautiful.
    Thanks a lot for you help! I will probably be able to fix this some time soon.
    Wish you a happy holiday season!
    You're most welcome.
    Glad to help (when I know what I'm doing)!
    Don't hesitate to ask about JS code provided.
    Good Luck and Happy Holidays to you as well.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles