Numbers and mathematics come into most scripts at some point, be it in the form of the simple arithmetic required to add up prices and work out sales tax, the process of generating and using random numbers, or the more complex mathematics involved animation.
Math.ceil : rounds a number upwards, so Math.ceil(8.23) returns 9
Math.floor : rounds a number downwards, so* Math.floor(23.8)* returns 23
Math.round : rounds the number to the nearest integer, so *Math.round(8.3) *returns 8, while Math.round(2.8) returns 3
Math.pow : raises one number to the power of another so *Math.pow(2,3)*returns
Math.sqrt : returns the square root of a number do *Math.sqrt(9) *returns 3
Math.random : returns a pseudorandom number between zero and one
In some applications, you may need to round the result to a certain number of decimal places. For instance, you may want to display temprature values in 0.1 degree increments, even you though your script has access to more precise values.
So the question is how to do it?
We can write a function that will round to any number of places, as follows:
So, for example, if we started with a number like n = 3.492867, we could call roundTo(n, 0) to get 3 or roundTo(n, 2) to get 3.49
Random numbers can be used as the basis for any task that needs to have a random or semi-random aspect.
We already know how to generate a random number between 0 and 1 by using Math.random() function but what if we need random numbers between 0-100 or maybe 8-23 or 23-1995 etc.
Well there is a solution
function to generate random number between any two numbers.
so, for example we could generate a number between 0 and 100 by calling randomBetween(0, 100), or a number between 8 and 1995 by calling radomBetween(8, 1995).
using randomBetween() function to get random numbers.
Once you’ve finished a calculation, you might want to turn the output into something more readable, such as formatting a value to represent currency.To do this we must convert the number to a string.
The most direct means of converting a number to a string is to built -in String constructor function:
converting number to string using built-in String constructor
converting string using toString() method
Another useful technique is string concatenation (joining strings together), which returns a string even if some of the input values are numbers:
Like Number to String, conversion of String to a Number is also possible.
The most direct means of converting a string to a number is to use the built-in Number constructor function:
conversion using built-in Number constructor function
Another handy technique is to use tha parseInt and ParseFloat functions, which will attempt to find and return an integer or decimal number at the start of a string.
conversion using parseInt and parseFloat
**note : **If the first character of the input is not a digit or some other numerical character , these functions will not be ale to return a number. In such cases. they’ll return the special value NaN (Not a Number).
Does your business need an interactive website or app?
The technology used to develop the overall app by the developers from WebClues Infotech is at par with the latest available technology.
For more inquiry click here https://bit.ly/31eZyDZ
Book Free Interview: https://bit.ly/3dDShFg
With 15+ years of domain expertise, we have successfully delivered 13800+ projects and have successfully garnered 6800+ happy customers with 97%+ client retention rate.
The compound assignment operators consist of a binary operator and the simple assignment operator.
The binary operators, work with two operands. For example a+b where + is the operator and the a, b are operands. Simple assignment operator is used to assign values to a variable(s).
It’s quite common to modify values stored in variables. To make this process a little quicker, we use compound assignment operators.
You can also check my video tutorial compound assignment operators.
Let’s consider an example. Suppose price = 5 and we want to add ten more to it.
var price = 5;
price = price + 10;
We added ten to price. Look at the repetitive price variable. We could easily use a compound += to reduce this. We do this instead.
price += 5;
Awesome. Isn’t it? What’s the value of price now? Practice and comment below. If you don’t know how to practice check these lessons.
Lets bring down the price by 5 again and display it.
We use console.log command to display what is stored in the variable. It is very help for debugging.
Debugging let’s you find errors or bugs in your code. More on this later.
price -= 5;
Lets multiply price and show it.
and finally we will divide it.
If you have any doubts, comment below.