Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

0012 oddToEven ( L-B )

Problem

Given an array of even and odd numbers, return an array of even, convert odd numbers to even.

Test Cases

  • Input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], Output: [2, 2, 4, 4, 6, 6, 8, 8, 10, 10]
  • Input: [1, 3, 5, 7, 9], Output: [2, 4, 6, 8, 10]
  • Input: [2, 4, 6, 8, 10], Output: [2, 4, 6, 8, 10]

Solution

const numbers = [12, 43, 11, 77, 60, 40, 45];

const oddToEven = arr => {
    const evenArr = arr.map(ele => {
        return ele % 2 !== 0 ? ele + 1 : ele
    })
    return evenArr;
}
oddToEven(numbers);

How it works

  • The function takes an array as a parameter.
  • It creates a variable called evenArr and assigns it a value of the array passed as a parameter.
  • Using the map method, it iterates through the array and checks if the element is odd or even.
  • If the element is odd, it adds 1 to it and returns it.
  • If the element is even, it returns it.
  • It returns the evenArr variable.

References

Problem Added By

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.