-
Notifications
You must be signed in to change notification settings - Fork 0
/
04) Data Type: Number.js
83 lines (63 loc) · 2.08 KB
/
04) Data Type: Number.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
1) Store Number:
a) Positive number:
const positiveNumber = 1234;
console.log(positiveNumber);
b) Negative number:
const negativeNumber = -1234;
console.log(negativeNumber);
c) Fractional number:
const fractionalNumber = 12.34;
console.log(fractionalNumber);
d) Challenge:
Create a variable named phoneNumber and store the number 1234567890 as the value.
Create another variable named pinCode and store the value 2255 in it.
At the end, put the values of both variables in the console.
Solution:
const phoneNumber = 1234567890;
const pinCode = 2255;
console.log(phoneNumber, pinCode);
2) NaN:
In JavaScript, NaN stands for Not a Number.
If we perform an arithmetic operation on a value that is not a number, we get NaN as the result.
Example:
const personID = "123abc";
const product = personID * 2;
console.log(product);
3) Increase or Decrease a Number by One:
a) Increment:
We can use ++ to increment the value of a number by 1.
b) Decrement:
We can use -- to decrement the value of a number by 1.
c) Example:
let firstNumber = 45; //will use let, as it's changes & const doesn't!
let secondNumber = 45;
firstNumber++;
secondNumber--;
console.log(firstNumber, secondNumber);
d) Challenge:
Create a variable firstNumber which stores the value of 3.
Create another variable secondNumber which stores the value 5.
Increase the value of firstNumber by one. Similarly, decrease the value of secondNumber by one.
Later check if firstNumber and secondNumber are equal or not, and log the result to the console.
Output should be a Boolean.
Solution:
let firstNumber = 3;
let secondNumber = 5;
firstNumber++;
secondNumber--;
let result = (firstNumber===secondNumber);
console.log(result);
4) Remainder:
We can use % to get the remainder.
It returns the remainder after the division of the value on the left of % by the value on the right of %.
Example:
a) console.log(15 % 3);
--> 0 // Remainder
b) console.log(16 % 3);
--> 1 //Remainder
c) Challenge:
Create a variable number and store the value 683 to it.
Display the remainder of division of number by 2.
Solution:
let number = 683;
console.log(number % 2);