Math Object In TypeScript: Part 5
Before reading this article, please go through the following articles:
Introduction
In this article, I am describing the TypeScript math functions Pow, Sqrt, and Random.
In TypeScript the pow() method is used to return the base to the exponent power. In this method we need to pass two parameters, the first is the base to which the power will be calculated and the other is the exponent value. The power parameter of the pow method can be a fractional value.
Pow() Method
In this example, we declare a variable v and it's value 2. We use this variable as an exponent.
Syntax
- pow(x,y)
where x is the number and y is the power parameter.
- Pow(num: number) {
- var v = 2;
- var span = document.createElement("span");
- span.style.color = "green";
- span.innerText = +v + " Power of random number is -> " + Math.pow(num, 2) + "\n";
- document.body.appendChild(span);
- }
Sqrt() Method
Syntax
- sqrt(number)
Function
- Sqrt(num: number) {
- var span = document.createElement("span");
- span.style.color = "blue";
- span.innerText = "Square Root of random number is -> " + Math.sqrt(num) + "\n";
- document.body.appendChild(span);
- }
Random() Method
Syntax
- random()
Function
- var random = Math.floor(Math.random() * 10);
Complete Program
Pow_Sqrt_Random.ts


Yusuf KaratoprakPosted Jan 14, 2013, 4:45 AM
Thanks. Good practice...