- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inverse operation in JavaScript
Let’s say, we have to write a function that takes in a binary string (consisting of only 0 and 1) and returns its inverse, all 0s replaced by 1 and 1s replaced by 0.
Let’s write the code for this function −
Example
const num = '1101'; const n = '11010111'; const inverseBinary = (binary) => { return binary.split("").map(el => { return `${1- parseInt(el, 10)}` }).join(""); }; console.log(inverseBinary(num)); console.log(inverseBinary(n));
Output
The output in the console will be −
0010 00101000
Advertisements