|
| 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. |
0 commit comments