-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArithmetic With JS
112 lines (82 loc) · 3.56 KB
/
Arithmetic With JS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Number is a data type in JavaScript which represents numeric data.
Now let's try to add two numbers using JavaScript.
JavaScript uses the + symbol as addition operation when placed between two numbers. */
var sum = 10 + 10; // Simple addition = 20
var difference = 45 - 33; // Simnple subtraction = 12
var product = 8 * 10; // Simple multiplication = 80
var quotient = 66 / 33; // Simple division = 2
/* You can easily increment or add one to a variable with the ++ operator.
i++; is the equivalent of i = i + 1;
Note - The entire line becomes i++;, eliminating the need for the equal sign. */
// Example of incrementation using the ++ operator removing the = sign
var myVar = 87;
myVar = myVar + 1; // Instead of this use the next example
myVar++; // This will increment the myVar variable making it equal to 88
/* You can easily decrement or decrease a variable by one with the -- operator.
i--; is the equivalent of i = i - 1;
Note - The entire line becomes i--;, eliminating the need for the equal sign. */
// Example of decrementation using the -- operator removing the = sign
var myVar = 11;
myVar = myVar - 1; // Instead of this use the next example
myVar--; // This will decrease the myVar variable making it equal to 10
/* We can store decimal numbers in variables too. Decimal numbers are sometimes referred to as floating point numbers or floats.
Note - Not all real numbers can accurately be represented in floating point. This can lead to rounding errors. Details Here. */
var myDecimal = 4.20; // Decimal/Floating point number
/* In JavaScript, you can also perform calculations with decimal numbers, just like whole numbers.
Let's multiply two decimals together to get their product. */
var product = 2.0 * 2.5; // This will give the product of 5
/* Now let's divide one decimal by another. */
var quotient = 4.4 / 2.0; // This quotient with equal 2.2
/* The remainder operator % gives the remainder of the division of two numbers.
Usage - In mathematics, a number can be checked to be even or odd by checking the remainder of the division of the number by 2.
17 % 2 = 1 (17 is Odd)
48 % 2 = 0 (48 is Even)
Note - The remainder operator is sometimes incorrectly referred to as the
"modulus" operator. It is very similar to modulus, but does not work properly with negative numbers. */
// Exammple of rainder operator % usage
var remainder;
remainder = 11 % 3; // This will set remainder to 1
/* In programming, it is common to use operators to modify the contents of a variable.
Remember that everything to the right of the equals sign is evaluated first, so we can say:
myVar = myVar + 5; to add 5 to myVar. Since this is such a common pattern, there are operators
which do both a mathematical operation and assignment in one step.
One such operator is the += operator. It is also true for -=, *=, and /=. */
Example:
var myVar = 1;
myVar += 5;
console.log(myVar); // Returns 6 */
// Usage example
var a = 3;
var b = 17;
var c = 12;
// we want the results shown below:
a = a + 12;
b = 9 + b;
c = c + 7;
// use this operator instead to achieve the same results with fewer keystrokes
a += 12;
b += 9;
c += 7;
// Example of -= operator
// Example of -= operator
var a = 11;
var b = 9;
var c = 3;
a -= 6;
b -= 15;
c -= 1;
// Example of *= operator
var a = 5;
var b = 12;
var c = 4.6;
a *= 5;
b *= 3;
c *= 10;
// Example of /= operator
var a = 48;
var b = 108;
var c = 33;
a /= 12;
b /= 4;
c /= 11;
// End of Simple arithmetic, incrementation, and decrementation, and operators