Skip to content

Commit 9bb1614

Browse files
committed
find bad data
1 parent 5d6b89f commit 9bb1614

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 0008 countBadData( L-B )
2+
3+
## Problem
4+
5+
Write a function that returns the number of bad data in an array.
6+
7+
## Test Cases
8+
9+
- countBadData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); // 0
10+
- countBadData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "L-B"]); // 1
11+
- countBadData([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "L-B", "L-B"]); // 2
12+
- countBadData([1, 2, 3, 4, 5, 6, 7, -8, 9, -10, "L-B", "L-B"]); // 4
13+
14+
## Solution
15+
16+
```javascript
17+
function countBadData(data) {
18+
let count = 0;
19+
for (const ele of data) {
20+
if (typeof ele !== "number") {
21+
count++;
22+
}
23+
}
24+
return count;
25+
}
26+
27+
const dataInput = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "L-B", "L-B"];
28+
const badData = countBadData(dataInput);
29+
```
30+
31+
32+
## How it works
33+
34+
- The function takes an array as a parameter.
35+
- It creates a variable called count and assigns it a value of 0.
36+
- It creates a for loop that iterates through the array.
37+
- It checks if the element is not a number.
38+
- If it is not a number, it increments the count variable by 1.
39+
- It returns the value of count.
40+
41+
## References
42+
43+
- [Wikipedia](https://en.wikipedia.org/wiki/Increment_and_decrement_operators)
44+
- [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)
45+
46+
## Problem Added By
47+
48+
- [GitHub](https://www.github.com/devvsakib)
49+
- [LinkedIn](https://www.linkedin.com/in/devvsakib)
50+
- [Twitter](https://twitter.com/devvsakib)
51+
52+
## Contributing
53+
54+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
55+
56+
Please make sure to update tests as appropriate.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// function findingBadData(arr) {
2+
// let badData = 0;
3+
// const err = "Given input must be Array";
4+
5+
// if (Array.isArray(arr)) {
6+
// for (let i = 0; i < arr.length; i++) {
7+
// if (arr[i] < 0) {
8+
// badData++;
9+
// }
10+
// }
11+
// return badData;
12+
// }
13+
// return err;
14+
// }
15+
function countBadData(data) {
16+
let count = 0;
17+
for (const ele of data) {
18+
if (typeof ele !== "number" || ele < 0) {
19+
count++;
20+
}
21+
}
22+
return count;
23+
}
24+
25+
const dataInput = [1, 2, 3, 4, -5, 6, 7, 8, 9, -10, "L-B", "L-B"];
26+
const badData = countBadData(dataInput);
27+
console.log(badData);

0 commit comments

Comments
 (0)