#29 - Ones' Complement CodeWars Kata (7 Kyu) - DEV Community

Add reaction Like Unicorn Exploding Head Raised Hands Fire Jump to Comments Save Boost More... Copy link Copy link Copied to Clipboard Share to X Share to LinkedIn Share to Facebook Share to Mastodon Share Post via... Report Abuse 1 #1 - Points of reflection (8 kyu) Codewars Kata 2 #2 - Hamming Distance CodeWars Kata (6 kyu) ... 43 more parts... 3 #3 - Wilson primes CodeWars Kata (8 kyu) 4 #4 - Pair of gloves CodeWars Kata (6 kyu) 5 #5 - People in the Bus CodeWars Kata (6 kyu) 6 #6 - Incrementer CodeWars Kata (7 kyu) 7 #7 - Closest and Smallest CodeWars Kata (5 kyu) 8 #8 - Simple remove duplicates CodeWars Kata (7 kyu) 9 #9 - Twin Prime CodeWars Kata (6 kyu) 10 #10 - Valid Spacing CodeWars Kata (7 kyu) 11 #11 - Vowel remover CodeWars Kata (8 kyu) 12 #12 - Fibonacci digit sequence CodeWars Kata (6 kyu) 13 #13 - Keypad Horror CodeWars Kata (7 kyu) 14 #14 - Keypad Horror CodeWars Kata (7 kyu) 15 #15 - Digits explosion CodeWars Kata (7 kyu) 16 #16 - Sums of parts CodeWars Kata (6 kyu) 17 #17 - The highest profit wins! CodeWars Kata (7 kyu) 18 #18 - Previous multiple of three CodeWars Kata (7 kyu) 19 #19 - Binary Addition CodeWars Kata (7 kyu) 20 #20 - Sort Numbers CodeWars Kata (7 kyu) 21 #21 - Sum of integers in string CodeWars Kata (7 kyu) 22 #22 - Find the unique number CodeWars Kata (6 kyu) 23 #23 - Change it up CodeWars Kata (6 kyu) 24 #24 - Create Phone Number CodeWars Kata (6 kyu) 25 #25 - Remove consecutive duplicate words CodeWars Kata (7 kyu) 26 #26 - Pillars CodeWars Kata (8 kyu) 27 #27 - Your order, please CodeWars Kata (6 kyu) 28 #28 - Sum of two lowest positive integers - CodeWars Kata (7 kyu) 29 #29 - Ones' Complement CodeWars Kata (7 kyu) 30 #30 - Multiplication table CodeWars Kata (6 kyu) 31 #31 - Sequences and Series CodeWars Kata (6 kyu) 32 #32 - Find the divisors! CodeWars Kata (7 kyu) 33 #33 -Derivatives of type x^n - CodeWars Kata (6 kyu) 34 #34 -Shortest code: Bug in Apple - CodeWars Kata (6 kyu) 35 #35 -(Ready for) Prime Time - CodeWars Kata (5 kyu) 36 #36 - Sum of Multiples CodeWars Kata (8 kyu) 37 #37 - Row of the odd triangle - CodeWars Kata (6 kyu) 38 #38 - Assemble string CodeWars Kata (6 kyu) 39 #39 - Two Sum CodeWars Kata (6 kyu) 40 #40 - Credit Card Mask Kata (7 kyu) 41 #41 - Split Strings Codewars Kata (6 kyu) 42 #42 - How many pages in a book? - Codewars Kata (6 kyu) 43 #43 - If you can read this... - Codewars Kata (6 kyu) 44 How can you help? 45 #44 - Where is my parent!?(cry) - Codewars Kata (6 kyu) 46 #45 - Persistent Bugger - Codewars Kata (6 kyu) 47 #46 - Sum of a sequence - 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

pic Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Submit Preview Dismiss Code of Conduct Report abuse

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.

Hide child comments as well

Confirm

For further actions, you may consider blocking this person and/or reporting abuse

Cesar Del rio Follow Join me on my road to mastering coding. 100 days of codewars challenge.
  • Location México
  • Work CEO at Mercurio Agencia Digital
  • Joined Dec 29, 2021
#46 - Sum of a sequence - Codewars Kata (7 kyu) #javascript #programming #100daysofcode #computerscience Any advice for Google STEP program? #beginners #programming #javascript #motivation #45 - Persistent Bugger - Codewars Kata (6 kyu) #javascript #programming #100daysofcode #computerscience
DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

Tag » Codewars 7 Kyu Solutions Javascript