Beginner JavaScript Tutorial – 36 – Math Objects




Facebook – https://www.facebook.com/TheNewBoston-464114846956315/
GitHub – https://github.com/buckyroberts
Google+ – https://plus.google.com/+BuckyRoberts
LinkedIn – https://www.linkedin.com/in/buckyroberts
reddit – https://www.reddit.com/r/thenewboston/
Support – https://www.patreon.com/thenewboston
thenewboston – https://thenewboston.com/
Twitter – https://twitter.com/bucky_roberts

Original source


33 responses to “Beginner JavaScript Tutorial – 36 – Math Objects”

  1. Question, why do I have to write the word new when doing:
    var VARNAMEHERE = NEW Array();

    but don't have to write it when I'm doing:
    var VARNAMEHERE = prompt();

    what does the word new do?

    thank you

  2. Thanks again Bucky! I m learning and test another methods of Math… like these…

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="ISO-8859-1">
    <title>Beginner Javascript Tutorial</title>
    <!– Beginner JavaScript Tutorial – 36 – Math Objects –>
    </head>
    <body>
    <script type="text/javascript">

    // document.write(Math.PI, "<br/>");
    // You may test the another methods behavior from MATH class. For example…
    // Remember to test diferents values like floats between "0.1, 0.2 + … + 0.9", floats ..1.4+… +4.5…

    var pi = Math.PI;
    var e = Math.E;
    document.write(pi, "<br/>");
    document.write(e, "<br/>");

    var n = prompt("Enter a number", "");

    var answerA = Math.sqrt(n);
    var answerB = Math.pow(2, n);
    var answerC = Math.abs(n);
    var answerD = Math.acos(n);
    var answerE = Math.asin(n);
    var answerF = Math.atan(n);
    var answerG = Math.ceil(n);
    var answerH = Math.cos(n);
    var answerI = Math.exp(n);
    var answerJ = Math.round(n);
    var answerK = Math.sin(n);
    var answerL = Math.tan(n);

    a = "The square root of " + n + " is " + answerA;
    b = "The base 2 exponent by " + n + " is " + answerB;
    c = "Return the absolute value of " + n + " is " + answerC;
    d = "Return the arccosine of n, in radians " + n + " is " + answerD;
    e = "Return the arcsine of n, in radians " + n + " is " + answerE;
    f = "Return the arctangent of n as a numeric value between -PI/2 and PI/2 radians " + n + " is " + answerF;
    g = "Return of n, rounded upwards to the nearest integer " + n + " is " + answerG;
    h = "Return the cosine of n (n is in radians) " + n + " is " + answerH;
    i = "Return the value of E^n " + n + " is " + answerI, "<br/>"
    j = "Return n, rounded downwards to the nearest integer " + n + " is " + answerJ;
    k = "Return the sine of n (n is in radians) " + n + " is " + answerK;
    l = "Return the tangent of an angle" + n + " is " + answerL;

    alert(a);
    alert(b);
    alert(c);
    alert(d);
    alert(e);
    alert(f);
    alert(g);
    alert(h);
    alert(i);
    alert(j);
    alert(k);
    alert(l);

    document.write(a + "<br>");
    document.write(b + "<br>" );
    document.write(c + "<br>" );
    document.write(d + "<br>" );
    document.write(e + "<br>" );
    document.write(f + "<br>" );

    // or printing using the method write one time only

    document.write(g + "<br>", h + "<br>", i + "<br>", j + "<br>", k + "<br>", l + "<br>" );

    </script>

    </body>
    </html>

  3. Can anyone figure why in the following code only the 1st document.write is executed but not the second ?

    <!DOCTYPE html>
    <html>
    <head></head>
    <body>
    <script type="text/javascript">
    document.write(Math.PI);

    var num = prompt("Enter a number:", "");
    var answer = Math.sqrt(num);
    document.write(answer);
    </script>
    </body>
    </html>

  4. Hey Bucky…when you used prompt like this:
    var n= prompt("enter your number"," ");
    …it means user is giving a String as input in the prompt box,right? so how a string can be used as a number? is it converting from string to integer automatically? i understand the fact,but i just wanna make sure.
    btw…huge fan of yours.
    thanks.

  5. Well Bucky, I'm still here, if I have trouble remembering all this, one thing I'll always remember, is how You can remember PI to so many places!
    PI will never be the same again, doesn't bare thinking about. 😉

  6. because Math.sqrt() is an inbuilt function. It was created by the developers of JavaScript and you dont have to re-write the Math.sqrt() function in your program again. It already exists and is ready to use. hope this was helpful

Leave a Reply