1//Math in JS is similar and different to the one in real life
2//Like: "+", "-" signs are the same. But "X" and "÷" are replaced with "*" and "/" respectivly
3//You can do math both with variables and normal numbers
4________________________________________________________________________________
5 Normal Number Examples
6
74+4 = 8;
84-4 = 0;
94*4 = 16;
104/4 = 1;
11________________________________________________________________________________
12 Variable Examples
13
14num1 = 0;
15num2 = 0;
16ans = 0;
17
18num1 = 4;
19num2 = 64
20
21num1 + num2 = 68
22num1 - num2 = 60
23num1 * num2 = 24
24num1 / num2 = 256;
25
26num1 + num2 = ans;
27num1 - num2 = ans;
28num1 * num2 = ans;
29num1 / num2 = ans;
30________________________________________________________________________________
31Now, you can use the document.getElementByID("").innerHTML = ans; to set the answer
32
33
34
35//I hope this post helped you!
1JS Numbers and Math:
2
3Number Properties:
4MAX_VALUE
5The maximum numeric value representable in JavaScript
6MIN_VALUE
7Smallest positive numeric value representable in JavaScript
8NaN
9The “Not-a-Number” value
10NEGATIVE_INFINITY
11The negative Infinity value
12POSITIVE_INFINITY
13Positive Infinity value
14
15Number Methods:
16toExponential()
17Returns a string with a rounded number written as exponential notation
18toFixed()
19Returns the string of a number with a specified number of decimals
20toPrecision()
21String of a number written with a specified length
22toString()
23Returns a number as a string
24valueOf()
25Returns a number as a number
26
27Math Properties:
28E Euler’s number
29LN2 The natural logarithm of 2
30LN10 Natural logarithm of 10
31LOG2E Base 2 logarithm of E
32LOG10E Base 10 logarithm of E
33PI The number PI
34SQRT1_2 Square root of 1/2
35SQRT2 The square root of 2
36
37Math Methods:
38abs(x)
39Returns the absolute (positive) value of x
40acos(x)
41The arccosine of x, in radians
42asin(x)
43Arcsine of x, in radians
44atan(x)
45The arctangent of x as a numeric value
46atan2(y,x)
47Arctangent of the quotient of its arguments
48ceil(x)
49Value of x rounded up to its nearest integer
50cos(x)
51The cosine of x (x is in radians)
52exp(x)
53Value of Ex
54floor(x)
55The value of x rounded down to its nearest integer
56log(x)
57The natural logarithm (base E) of x
58max(x,y,z,...,n)
59Returns the number with the highest value
60min(x,y,z,...,n)
61Same for the number with the lowest value
62pow(x,y)
63X to the power of y
64random()
65Returns a random number between 0 and 1
66round(x)
67The value of x rounded to its nearest integer
68sin(x)
69The sine of x (x is in radians)
70sqrt(x)
71Square root of x
72tan(x)
73The tangent of an angle
1function sum(a, b) {
2 return a + b;
3}
4
5function subtract(a, b) {
6 return a - b;
7}
8
9function multiply(a, b) {
10 return a * b;
11}
12
13function divide(a, b) {
14 if (b === 0) {
15 console.log("can't divide by zero");
16 }
17
18 if (a === 0 && b === 0) {
19 console.log("undetermined");
20 }
21
22 //or this without any IFs
23 return a / b;
24}
25
1
2Math.ceil(4.9); // returns 5
3
4Math.ceil(4.7); // returns 5
5
6Math.ceil(4.4); // returns 5
7
8Math.ceil(4.2); // returns 5
9
10Math.ceil(-4.2); // returns -4
11
12
1// functions and constants
2math.round(math.e, 3) // 2.718
3math.atan2(3, -3) / math.pi // 0.75
4math.log(10000, 10) // 4
5math.sqrt(-4) // 2i
6math.derivative('x^2 + x', 'x') // 2*x+1
7math.pow([[-1, 2], [3, 1]], 2)
8 // [[7, 0], [0, 7]]
9
10// expressions
11math.evaluate('1.2 * (2 + 4.5)') // 7.8
12math.evaluate('12.7 cm to inch') // 5 inch
13math.evaluate('sin(45 deg) ^ 2') // 0.5
14math.evaluate('9 / 3 + 2i') // 3 + 2i
15math.evaluate('det([-1, 2; 3, 1])') // -7
16
17// chaining
18math.chain(3)
19 .add(4)
20 .multiply(2)
21 .done() // 14
1
2Math.sign(-4); // returns -1
3
4Math.sign(0); // returns 0
5
6Math.sign(4); // returns 1
7
8