|
| 1 | +# 0012 oddToEven ( L-B ) |
| 2 | + |
| 3 | +## Problem |
| 4 | + |
| 5 | +Given an array of even and odd numbers, return an array of even, convert odd numbers to even. |
| 6 | + |
| 7 | +## Test Cases |
| 8 | + |
| 9 | +- Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], Output: [2, 2, 4, 4, 6, 6, 8, 8, 10, 10] |
| 10 | +- Input: [1, 3, 5, 7, 9], Output: [2, 4, 6, 8, 10] |
| 11 | +- Input: [2, 4, 6, 8, 10], Output: [2, 4, 6, 8, 10] |
| 12 | + |
| 13 | +## Solution |
| 14 | + |
| 15 | +```javascript |
| 16 | + |
| 17 | +const numbers = [12, 43, 11, 77, 60, 40, 45]; |
| 18 | + |
| 19 | +const oddToEven = arr => { |
| 20 | + const evenArr = arr.map(ele => { |
| 21 | + return ele % 2 !== 0 ? ele + 1 : ele |
| 22 | + }) |
| 23 | + return evenArr; |
| 24 | +} |
| 25 | +oddToEven(numbers); |
| 26 | + |
| 27 | +``` |
| 28 | + |
| 29 | +## How it works |
| 30 | + |
| 31 | +- The function takes an array as a parameter. |
| 32 | +- It creates a variable called evenArr and assigns it a value of the array passed as a parameter. |
| 33 | +- Using the map method, it iterates through the array and checks if the element is odd or even. |
| 34 | +- If the element is odd, it adds 1 to it and returns it. |
| 35 | +- If the element is even, it returns it. |
| 36 | +- It returns the evenArr variable. |
| 37 | + |
| 38 | +## References |
| 39 | + |
| 40 | +- [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) |
| 41 | +- [Wikipedia](https://en.wikipedia.org/wiki/Map_(higher-order_function)) |
| 42 | +- [GeeksforGeeks](https://www.geeksforgeeks.org/javascript-array-map-method/) |
| 43 | + |
| 44 | +## Problem Added By |
| 45 | + |
| 46 | +- [GitHub](https://www.github.com/devvsakib) |
| 47 | +- [LinkedIn](https://www.linkedin.com/in/devvsakib) |
| 48 | +- [Twitter](https://twitter.com/devvsakib) |
| 49 | + |
| 50 | +## Contributing |
| 51 | + |
| 52 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
| 53 | + |
| 54 | +Please make sure to update tests as appropriate. |
0 commit comments