DEV Community

Cesar Del rio
Cesar Del rio

Posted on • Updated on

#29 - Ones' Complement CodeWars Kata (7 kyu)

Instructions

The Ones' Complement of a binary number is the number obtained by swapping all the 0s for 1s and all the 1s for 0s. For example:

onesComplement(1001) = 0110
onesComplement(1001) = 0110

For any given binary number,formatted as a string, return the Ones' Complement of that number.


My solution:

function onesComplement(n) {
  return n.split('').map(n=>n == '0' ? '1' : '0').join('')
};
Enter fullscreen mode Exit fullscreen mode

Explanation

I splitted the string into an array, and the I mapped it, using a ternary conditional I checked if the number being iterated is '0' it will change it to '0', else it'll change it to '0', and at the end I just joined the array.


What do you think about this solution? 👇🤔

My Github
My twitter
Solve this Kata

Discussion (0)