Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 29

Thread: Simple Math

  1. #1
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple Math

    Hi,

    I am trying to create a javascript that says:

    add a and b together and then divide it by 2.
    (a + b) / 2

    How would you do this?
    - Saj

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts

    solution using functions

    Code:
    function add(num1, num2) {
      sum = num1 + num2;
      return sum
    }
    
    function halve(num) {
      half = num/2;
      return half;
    }
    
    function sumAndHalve(numA, numB) {
      numC = add(numA, numB);
      numD = halve(numC);
      return numD
    }
    then, anywhere, you'd call
    Code:
    sumAndHalve(3,1)
    I know there are easier, more terse ways to code this, but I like to keep each operation separate. Is that so wrong?
    Last edited by brothercake; 04-01-2003 at 10:07 PM.

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy D'oh!

    I posted a new thread instead of posting a reply. D'oh! Check it.


    <edit by="brothercake">
    I merged your answer (above) into this thread
    </edit>
    Last edited by brothercake; 04-01-2003 at 10:08 PM.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or make some methods! I love methods!

    Code:
    Number.prototype.add = function()
    {
    	var arg, i = 0, sum = this;
    	while( arg = arguments[i++] )
    	{
    		sum += parseInt( arg, 10 );
    	}
    	return sum;
    }
    
    Number.prototype.divide = function( denom )
    {
    	return this / denom;
    }
    
    var a = 4;
    var b = 2;
    
    alert( a.add( b ).divide( 2 ) );
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks, brothercake.

    beetle, great solution. For some reason, I don't seem to consider extending the prototype until long after the original need is fulfilled by functions. Dur!

  • #6
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok thanks. would i be able to use variables already defined as the addends?
    - Saj

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Wouldn't this be cooler to have on an Array?
    Code:
    Array.prototype.sum=function(){
        var
            l=this.length,
            r=0;
        while(--l)
            r+=this.[l];
        return r;
    }
    
    Array.prototype.avg=function(){
        var
            k,
            l=k=this.length,
            r=0;
        while(--l)
            r+=this.[l];
        return (r/k);
    }
    Then you can use [a,b].avg() for doing the job.
    Last edited by liorean; 04-01-2003 at 10:26 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cooler? Yes? More practical? Your mileage may vary

    Choopernickel - extending the prototype is a large part of an upcoming article I'm writing for Sitepoint.com on javascript namespaces/scope It should be an eye-opener for many (I hope)
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #9
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Excellent, beetle. I've gotten familiar with extending it through some string operations I've come up with, then with an Array.search() method that I think was pretty cool.

    I'm SO freakin' happy to finallly have found a javascript forum where it's like, all serious javascripting and everything! like you don't even know.

  • #10
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok here's what I'm trying to do:

    Code:
    Number.prototype.add = function() { 
      var arg, i = 0, sum = this; 
      while( arg = arguments[i++] ) { 
        sum += parseInt( arg, 10 ); 
      } return sum; 
    } 
    
    Number.prototype.divide = function( denom ) { 
      return this / denom; 
    } 
    
    function calculate() {  
      var a = document.triangle.a.value
      var b = document.triangle.b.value
      var c = document.triangle.c.value
      var b2 = b*b
      var c2 = c*c
    
      if (a=" ") {
      document.triangle.a.value = b2.add(c2).divide( 2 ) 
      }
      else {
      javascript:calculate2()
      }
    }


    For this one I want c - b, not c + b. I've spent ages on this, and can't firure it out.
    Last edited by Saj; 04-01-2003 at 11:30 PM.
    - Saj

  • #11
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    LOL Never Mind stupid question. I think I got it.
    - Saj

  • #12
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Please tell me what I am doing wrong here!

    Code:
    function divide(num) {
       half = num/2;
      return half;
    }
    
    function calculate() {  
      var a = document.triangle.a.value;
      var b = document.triangle.b.value;
      var c = document.triangle.c.value;
      var b2 = b*b;
      var c2 = c*c;
      var a2 = a*a;
      var c2b2 = c2-b2;
    
      if (a=" ") {
        document.triangle.a.value = divide(c2b2);
      }
      else {
        alert("Hello");
      }
    }
    By the way, yes I am doing the pythagorean theorem. A few of the variables are not necessary now, but I need them later.
    - Saj

  • #13
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    All values retrieved from form elements are String-type variables. You need to convert them to Number-type with parseInt

    var f = document.triangle;
    var a = parseInt( f.a.value, 10 );
    var b = parseInt( f.b.value, 10 );
    var c = parseInt( f.c.value, 10 );

    Which is why I suspect you also had problem using methods prototyped onto the Number object
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #14
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I see what your doing, but what is the need for the 10?
    - Saj

  • #15
    Saj
    Saj is offline
    Regular Coder
    Join Date
    Feb 2003
    Posts
    181
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, I'm still getting an answer of 4.5, when I want three, with this code:

    Code:
    function divide(num) {
       half = num/2;
      return half;
    }
    
    function calculate() {  
      var triangle = document.triangle; 
      var a = parseInt( triangle.a.value, 10 ); 
      var b = parseInt( triangle.b.value, 10); 
      var c = parseInt( triangle.c.value, 10); 
      var a2 = a*a;
      var b2 = b*b;
      var c2 = c*c;
      var c2b2 = c2-b2;
    
      if (a=" ") {
        document.triangle.a.value = divide(c2b2);
      }
      else {
        alert("Hello");
      }
    }
    - Saj


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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