code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

025 Character Parity

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Given a character, check if it represents an odd digit, an even digit or not a digit at all.

Example

For symbol = '5', the output should be characterParity(symbol) = "odd";
For symbol = '8', the output should be characterParity(symbol) = "even";
For symbol = 'q', the output should be characterParity(symbol) = "not a digit".
*/

function characterParity(value) {
  if (!isNaN(parseInt(value))) {
    return value % 2 == 0 ? 'even' : 'odd'
  }
  return 'not a digit'
}

test('character Parity', () => {
  expect(characterParity(5)).toEqual('odd')
  expect(characterParity(8)).toEqual('even')
  expect(characterParity('q')).toEqual('not a digit')
});

Created 2019-12-09T03:27:46+05:30 · Edit