Tuesday, December 25, 2012

Math function


This post will demonstrate the commonly used Math functions.

1. abs() - this function is used to provide the absolute value(which ignores the negative sign). you can try this function by clicking the button and entering a value to find its absolute value.The code is avaliable

<html>
      <input name="button" onclick="abs()" type="button" value="Absolue" />
      <script type="text/javascript">
      function abs(){
       absvalue = prompt("Please enter a number:", " ");
       alert('Absolute value of the number::'+Math.abs(absvalue));
       }
      </script>
</html>




2. ceil() - This function provides the next highest integer value.
Click on the ceil button to explore the functionality.


<html>
      <input onclick="ceil()" type="button" value="ceil" />
      <script type="text/javascript">
      function ceil(){
       ceilvalue = prompt("Please enter a number:");
       alert('Ceil value of the number is::'+Math.ceil(ceilvalue));
       }
      </script>
</html>



3. floor() - This function provides the next lowest integer value.
Click on the floor button to explore the functionality.


<html>
      <input onclick="floor()" type="button" value="floor" />
      <script type="text/javascript">
      function floor(){
       floorvalue = prompt("Please enter a number:");
       alert('Floor value of the number is::'+Math.floor(floorvalue));
       }
      </script>
</html>


Please note while using the ceil and floor function with (negative (-) values) the logic is different for example ceil value of -45.9 is -45 and not -46, similarly floor value of -27.8 is -28 and not -27.



5. random() - this number is used to generate random numbers of no particular order.
 Click on the button and check that every time you are displayed with different values.


<html>
      <input onclick="random()" type="button" value="Random" />
      <script type="text/javascript">
      function random(){
       alert('Random number::'+Math.random());
       }
      </script>
</html>



6. max() - This function aids in finding the maximum value out of the given values.


<html>
  <script type="text/javascript">
      function max1(){
       var t1=document.for1.txt1.value;
       var t2=document.for1.txt2.value;
       var t3=document.for1.txt3.value;
       alert('Max value of the number is::'+Math.max(t1,t2,t3));
       }
   </script>
<form name="for1">
Enter the first number:<input name="txt1" type="text" /><br />
Enter the second number:<input name="txt2" type="text" /><br />
Enter the Third number:<input name="txt3" type="text" /><br />
      <input onclick="max1()" type="button" value="Max value" />
</form>
</html>


Enter the first number:
Enter the second number:
Enter the Third number:


7. min() - This function aids in finding the minimum value out of the given values.


<html>
  <script type="text/javascript">
      function min1(){
       var t1=document.for2.tx1.value;
       var t2=document.for2.tx2.value;
       var t3=document.for2.tx3.value;
       alert('Max value of the number is::'+Math.min(t1,t2,t3));
       }
   </script>
<form name="for2">
Enter the first number:<input name="tx1" type="text" /><br />
Enter the second number:<input name="tx2" type="text" /><br />
Enter the Third number:<input name="tx3" type="text" /><br />
      <input onclick="min1()" type="button" value="Minimum value" />
</form>
</html>



Enter the first number:
Enter the second number:
Enter the Third number:

8. round() - This function rounds to the next integer value.
Click on the round button to explore the functionality.



<html>
      <input onclick="round()" type="button" value="Round" />
      <script type="text/javascript">
      function round(){
       roundvalue = prompt("Please enter a number:");
       alert('Round value of the number is::'+Math.round(roundvalue));
       }
      </script>
</html>
Informative Javascript

No comments:

Post a Comment