Expressions help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mwh

    Expressions help

    Hi.

    I am making a calculator kind of calculator using javascript, and I am
    trying to add numbers together. Example:

    function additionCalcula tor(num1, num2){
    var total = num1+num2
    return total
    }

    I use this script to add num1 and num2 together. But, it just puts them
    together, it does not add them together.

    I also want to subtract and divide in my calculator, and I don't know
    how to use these in JavaScript.

    Can anyone help?

    (____)
    (\/)
    /-------\/
    / | Mwh ||
    - ||----||

  • David

    #2
    Re: Expressions help

    It depends on how your calling the args. It will add the numbers correctly
    if they are actual numbers so in your function call do not use any
    quotations..

    additionCalcula tor(10,20)

    If you need to use quotations you'll have to parse the numbers because they
    are considered as "text" and not actual numbers.

    var total = parseInt(num1)+ parseInt(num2);

    additionCalcula tor('10','20')

    David





    "mwh" <a_man_dude@yah oo.com> wrote in message
    news:1115253959 .395520.291540@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > Hi.
    >
    > I am making a calculator kind of calculator using javascript, and I am
    > trying to add numbers together. Example:
    >
    > function additionCalcula tor(num1, num2){
    > var total = num1+num2
    > return total
    > }
    >
    > I use this script to add num1 and num2 together. But, it just puts them
    > together, it does not add them together.
    >
    > I also want to subtract and divide in my calculator, and I don't know
    > how to use these in JavaScript.
    >
    > Can anyone help?
    >
    > (____)
    > (\/)
    > /-------\/
    > / | Mwh ||
    > - ||----||
    >[/color]


    Comment

    • Vic Sowers

      #3
      Re: Expressions help


      "mwh" <a_man_dude@yah oo.com> wrote in message
      news:1115253959 .395520.291540@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      > Hi.
      >
      > I am making a calculator kind of calculator using javascript, and I am
      > trying to add numbers together. Example:
      >
      > function additionCalcula tor(num1, num2){
      > var total = num1+num2
      > return total
      > }
      >
      > I use this script to add num1 and num2 together. But, it just puts them
      > together, it does not add them together.
      >
      > I also want to subtract and divide in my calculator, and I don't know
      > how to use these in JavaScript.[/color]

      function additionCalcula tor(num1, num2) {
      var total = Number(num1)+Nu mber(num2);
      return total;
      }


      Comment

      • Zifud

        #4
        Re: Expressions help

        mwh wrote:[color=blue]
        > Hi.
        >
        > I am making a calculator kind of calculator using javascript, and I am
        > trying to add numbers together. Example:
        >
        > function additionCalcula tor(num1, num2){
        > var total = num1+num2
        > return total
        > }
        >
        > I use this script to add num1 and num2 together. But, it just puts them
        > together, it does not add them together.
        >
        > I also want to subtract and divide in my calculator, and I don't know
        > how to use these in JavaScript.[/color]

        <URL:http://www.jibbering.c om/faq/#FAQ4_21>

        --
        Zif

        Comment

        • Dr John Stockton

          #5
          Re: Expressions help

          JRS: In article <wZeee.1648$Vu. 1318@trnddc07>, dated Thu, 5 May 2005
          01:50:20, seen in news:comp.lang. javascript, David <right@dd.com > posted
          :[color=blue]
          >It depends on how your calling the args. It will add the numbers correctly
          >if they are actual numbers so in your function call do not use any
          >quotations..
          >
          >additionCalcul ator(10,20)
          >
          >If you need to use quotations you'll have to parse the numbers because they
          >are considered as "text" and not actual numbers.
          >
          >var total = parseInt(num1)+ parseInt(num2);
          >
          >additionCalcul ator('10','20')[/color]

          You've only just arrived here; you need to look around a bit in order to
          be sure of giving sound advice. Read the newsgroup FAQ, learn how to
          format a news reply (Sec 2.3), learn why parseInt should only rarely be
          used without a radix (4.12), and why it is not needed (4.21). See
          signature.
          [color=blue]
          >"mwh" <a_man_dude@yah oo.com> wrote in message
          >news:111525395 9.395520.291540 @g14g2000cwa.go oglegroups.com. ..[color=green]
          >> Hi.
          >> ...[/color][/color]

          var total = +num1 + +num2 // or = + num1 + + num2
          will suffice. The + which are adjacent to the letter n can only be
          unary-plus, the output of which is always a number.

          Given that the inputs probably cone from a control, it's probably best
          to "capture" them with lines like

          var Num1 = + document ... Num1Input.value

          and then work with Num1 - after reading the FAQ notes on Form Access
          and/or Square Brackets.

          --
          © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
          <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
          <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
          <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

          Comment

          • David

            #6
            Re: Expressions help

            "Dr John Stockton" <spam@merlyn.de mon.co.uk> wrote in message
            news:bRxqIuKUAq eCFwhx@merlyn.d emon.co.uk...
            [color=blue]
            > You've only just arrived here; you need to look around a bit in order to
            > be sure of giving sound advice. Read the newsgroup FAQ, learn how to
            > format a news reply (Sec 2.3), learn why parseInt should only rarely be
            > used without a radix (4.12), and why it is not needed (4.21). See
            > signature.[/color]



            Points taken .. David


            Comment

            Working...