-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaN.js
More file actions
179 lines (135 loc) · 4.45 KB
/
Copy pathNaN.js
File metadata and controls
179 lines (135 loc) · 4.45 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
console.log(' NaN: Not A Number ');
// NaN is the property of global object OR Variable in the Global Scope
//Operations that produced NaN
/*
1. Failed Number conversion i.e: Number("This is not a Number")
2. Math operation where the result is not a real number i.e: Math.sqrt(-1)
3. Any illegal mathematical operation in JS i.e: 0 * Infinity, infinity / infinity
4. Method or expression whose operands is NaN or coerced to NaN i.e: (1 * 'abc'), 0 ** NaN
5. When we try to represent invalid value to numbers i.e: new Date('This is Invalid').getTime()
*/
console.log(Number("This is not a Number"))
console.log(Math.sqrt(-1));
console.log(0 * Infinity); console.log(Infinity - Infinity); console.log(1 ** Infinity)
console.log(0 * NaN); console.log(1 * 'abc')
console.log(new Date('Invalid Date').getTime());
//IMPORTANT: NaN is not equal to NaN it self
console.log(NaN === NaN)
console.log(Number.NaN === NaN)
console.log(isNaN(NaN))
console.log(Number.isNaN(NaN))
console.log(Number.isNaN('NotANumber')) //false: Because it only checks if it actually a NaN
console.log(isNaN('NotANumber'))
//when try to check isNaN using BigInt it throws errors because BigInt to Number is not allowed
//isNaN(1n) //error: becuase the global isNaN function try to convert provided value to number and then check if it is NaN or not
console.log(Number.isNaN(1n)) //false: Because ut only checks if it is NaN or not without converting
/* Some Array Methods are also unable to find NaN while some can */
//Index finding cannot find the NaN
const array = [1,2,3,4, NaN];
console.log(array.indexOf(4)); //3
console.log(array.indexOf(NaN)); //-1
console.log(array.lastIndexOf(NaN))
//Value finding can find the NaN
console.log(array.includes(NaN));
console.log(array.findIndex(n => Number.isNaN(n))) //4
/* Brainstroming */
/*
🧠 Brainstorming Task: Transaction Analyzer — NaN Edition
You receive transaction data where amount can be:
a valid number
NaN
a numeric string
a non-numeric string
null
undefined
Infinity
-Infinity
Your job is to build a function:
function analyzeTransactions(transactions) {
// your implementation
}
Example input:
const transactions = [
{ id: 1, amount: 1500 },
{ id: 2, amount: NaN },
{ id: 3, amount: "2500" },
{ id: 4, amount: "abc" },
{ id: 5, amount: null },
{ id: 6, amount: undefined },
{ id: 7, amount: Infinity },
{ id: 8, amount: -Infinity },
{ id: 9, amount: 500 }
];
Your analyzer should produce something conceptually like:
{
validNumbers: 2,
nanValues: 1,
numericStrings: 1,
invalidStrings: 1,
nullValues: 1,
undefinedValues: 1,
infiniteValues: 2,
totalValidAmount: 2000,
averageValidAmount: 1000
}
*/
function analyzeTransactions(transactions) {
const result = {
validNumbers: 0,
nanValues: 0,
numericStrings: 0,
invalidStrings: 0,
nullValues: 0,
undefinedValues: 0,
infiniteValues: 0,
totalValidAmount: 0,
averageValidAmount: 0
}
transactions.forEach(transaction => {
const {amount} = transaction;
const type = typeof amount;
if(type === 'number'){
if(Number.isFinite(amount)){
result.validNumbers++;
result.totalValidAmount += amount;
result.averageValidAmount = result.totalValidAmount / result.validNumbers;
}
else if(Number.isNaN(amount)){
result.nanValues++;
}
else{
result.infiniteValues++;
}
}
else if(type === 'string' ){
(amount.trim().length && !isNaN(amount)) ? result.numericStrings++ : result.invalidStrings++;
}
else if(type === 'undefined'){
result.undefinedValues ++;
}
else if(amount === null){
result.nullValues ++;
}
});
return result;
}
const transactions = [
{ id: 1, amount: 1500 },
{ id: 2, amount: NaN },
{ id: 2, amount: NaN },
{ id: 2, amount: NaN },
{ id: 3, amount: "2500" },
{ id: 4, amount: "abc" },
{ id: 5, amount: null },
{ id: 5, amount: null },
{ id: 5, amount: 'null' },
{ id: 6, amount: undefined },
{ id: 6, amount: undefined },
{ id: 6, amount: undefined },
{ id: 6, amount: undefined },
{ id: 7, amount: Infinity },
{ id: 8, amount: -Infinity },
{ id: 9, amount: 500 }
];
const analyzer = analyzeTransactions(transactions);
console.log(analyzer)