1
+ /* 8. Numbers And Maths
2
+
3
+ (i) Numbers,
4
+ When Javascript automatically defines the data type, */
5
+ const balance = 400
6
+ // console.log(balance); // Output: 400
7
+ // Note: Here Javascript automatically detected that the score type is number.
8
+
9
+ // But we can also define explicitely the data type,
10
+ const newBalance = new Number ( 400 )
11
+ // console.log(newBalance); // Output: [Number: 400]
12
+ // Note: When we do this in browser console the we can see the methods.
13
+
14
+ // ---------------------------------------------------------------------------------------------------------
15
+
16
+ // Some Methods,
17
+ // console.log(newBalance.toString()); // Output: 400
18
+ // Note: When we will check it's typeof the we will see that it is changed into strings.
19
+ // console.log(newBalance.toString().length); // Output: 3 (cause 100 has 3 chracters.)
20
+
21
+ // console.log(newBalance.toFixed(2)); // Output: 400.00
22
+
23
+ const otherNumbers = 123.8966
24
+ // console.log(otherNumbers.toPrecision(3)); // Output: 124
25
+ const otherNum = 23.8966
26
+ // console.log(otherNum.toPrecision(3)); // Output: 23.9
27
+ // Note: It means we want precise value but on how many values we want to focus.
28
+
29
+ const hundreds = 1000000
30
+ // console.log(hundreds.toLocaleString('en-IN')); // Output: 10,00,000 (converted accor. to IN standards.)
31
+
32
+ /* *******************************************MATHS*******************************************************
33
+
34
+ (ii) Maths, */
35
+ console . log ( Math ) ; /* Output: Object [Math] {}
36
+ Note: When we run this console.log(Math) in browser's console we will get value under object. */
37
+
38
+ console . log ( Math . abs ( - 4 ) ) ; // Output: 4 (Changes only minus values in plus)
39
+
40
+ console . log ( Math . round ( 4.6 ) ) ; // Output: 5
41
+ console . log ( Math . round ( 4.4 ) ) ; // Output: 4
42
+ console . log ( Math . ceil ( 4.2 ) ) ; // Output: 5 (Choose top value)
43
+ console . log ( Math . floor ( 4.9 ) ) ; // Output: 4 (Choose lowest value)
44
+
45
+ console . log ( Math . min ( 3 , 2 , 4 , 6 ) ) ; /* Output: 2 (Tells min value)
46
+ Note: There is also "max". */
47
+
48
+ console . log ( Math . random ( ) ) ; // Output: 0.36675387800250636 (Gives random numbers between 0 & 1)
49
+ console . log ( ( Math . random ( ) * 10 ) + 1 ) ; // Now numbers will range from 1 to 9
50
+ console . log ( Math . floor ( Math . random ( ) * 10 ) + 1 ) ; // Round offs at Random numbers at lowest value.
51
+
52
+ const min = 10
53
+ const max = 20
54
+ console . log ( Math . floor ( Math . random ( ) * ( max - min + 1 ) ) + min ) ;
55
+ // Note: (max - min) to get a range, (+ 1) to avoid zero, (+ min) to get minimum value to this.
0 commit comments