SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Foolling around!

    I was just fooling around with this script and wondered if there was a more efficient way of doing it! It seems like slot of code for just switching the operator! any one have any ideas!

    Code:
    <script>
    function addMyValue(val_1,val_2){
    myValue_1=Number(val_1);
    myValue_2=Number(val_2);
    x=document.theFrm.oper.selectedIndex;
    if(0==x){
    myValue_1=myValue_1*myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    else if(1==x){
    myValue_1=myValue_1-myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    else if(2==x){
    myValue_1=myValue_1+myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    else if(3==x){
    myValue_1=myValue_1%myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    else if(4==x){
    myValue_1=myValue_1/myValue_2;
    document.theFrm.ans.value=myValue_1;
    }
    }
    </script>
    
    <form name="theFrm">
    <input type="text" name="val1" onblur="this.form.ans.value=this.value;"> 
    <select name="oper">
    <option>*
    <option>-
    <option>+
    <option>%
    <option>/
    </select>
    <input type="text" name="val2" onblur="addMyValue(this.form.val1.value,this.form.val2.value)"> =
    <input type="text" name="ans" onfocus="addMyValue(this.form.val1.value,this.form.val2.value)">
    </form>

  2. #2
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd start by dumping all those if-elses, and using a switch statement:

    Code:
     
    switch(x) {
      case 0: 
    	// do stuff
    	break;
      case 1: 
    	break;  
      ...
    }
    A bit prettier, but it could probably be even nicer than that since the code you're executing is practically identical per 'x' value...

  3. #3
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd add *,/,+,- as values to the options too, then simply use a couple of lines of code to dynamically execute the operation.... something like this (untested):
    Code:
    f=document.theFrm;
    op=f.oper.options[f.oper.selectedIndex].value;
    f.ans.value = eval(Number(val_1) + op + Number(val_2))
    (i have left your calls to the Number() functions as they were... I assume that they validate the values of the text boxes?)

    Last edited by M@rco; Aug 31, 2003 at 06:40.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Poop,

    If you're not adverse to using "eval()", here's a 3-liner (without error checking)

    Code:
    function addIt(val1,val2)
    {
      var selObj = document.theFrm.oper
      var x = selObj.options[selObj.selectedIndex].value;
    
    document.theFrm.ans.value=eval(parseInt(val1) + x + parseInt(val2))
    }
    </script>
    <form name="theFrm">
    <input type="text" name="val1"> 
    <select name="oper">
    <option value="*">*
    <option value="-">-
    <option value="+">+
    <option value="%">%
    <option value="/">/
    </select>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Er, Vincent... great minds and all that... but... look up...!!!
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Marcus,

    What can I say??? ops: ????

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I said... great minds...
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks guys! The reason that i am using Number is because it allows floating point integers!


Bookmarks

Posting Permissions

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