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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    23
    Thanks
    13
    Thanked 0 Times in 0 Posts

    javascript to the power of

    hello

    quick question how do i do to the power of in javascript

    eg

    2 to the power of 2 = 4

    thanks
    Last edited by rhyz; 06-20-2009 at 02:21 PM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Code:
    var num = 2;
    var exponent = 3;
    var result = Math.pow(num,exponent);    // 8

    It is your responsibility to die() if necessary….. - PHP Manual

  • Users who have thanked Philip M for this post:

    rhyz (06-20-2009)

  • #3
    Regular Coder
    Join Date
    May 2009
    Location
    China
    Posts
    133
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rhyz View Post
    hello

    quick question how do i do to the power of in javascript

    eg

    2 to the power of 2 = 4

    thanks
    The answer should be in any very basic javascript book under "math functions" or "math operators".

    ...but since you asked


    Code:
    var Base = 2;
    var Power = 2;
    var  Answer = Base ^ Power;

    there ya go

  • Users who have thanked KevinJohnson for this post:

    rhyz (06-20-2009)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,985
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by KevinJohnson View Post
    Code:
    var Base = 2;
    var Power = 2;
    var  Answer = Base ^ Power;
    Wrong!

    In Javascript ^ means bitwise exclusive OR, e.g. 10^3=9

  • Users who have thanked Philip M for this post:

    rhyz (06-20-2009)

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    23
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks !!!

    i thought ^ to start but when i tried i get 0

    silly me


    Thanks !!!


  •  

    Posting Permissions

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