

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript - Complementary Colors Builder
We are required to write a JavaScript function that takes in hex color as one and only input.
Our function should then find the complementary color for the color taken in as input.
Here are some input and output pairs −
getComplementaryColor('#142814') = '#ebd7eb'; getComplementaryColor('#ffffff') = '#000000'; getComplementaryColor('#3399ff') = '#cc6600';
Example
The code for this will be −
const str1 = '#142814'; const str2 = '#ffffff'; const str3 = '#3399ff'; const getComplementaryColor = (color = '') => { const colorPart = color.slice(1); const ind = parseInt(colorPart, 16); let iter = ((1 << 4 * colorPart.length) - 1 - ind).toString(16); while (iter.length < colorPart.length) { iter = '0' + iter; }; return '#' + iter; }; console.log(getComplementaryColor(str1)); console.log(getComplementaryColor(str2)); console.log(getComplementaryColor(str3));
Output
And the output in the console will be −
#ebd7eb #000000 #cc6600
- Related Questions & Answers
- Complementary Code Keying (CCK)
- IntStream builder() method in Java
- DoubleStream builder() method in Java
- LongStream builder() method in Java
- How to implement Builder pattern in Kotlin?
- How to use string Builder class in android?
- Can you give some complementary feeding tips for babies?
- Defining Colors in CSS3
- Sort Colors in Python
- Difference between String buffer and String builder in Java
- Program to find minimum moves to make array complementary in Python
- Which is best a web designer or a web builder
- Generate colors between #CCCCCC and #3B5998 for a color meter with JavaScript?
- How to show that regular language is closed under the complementary operation?
- Print Colors of terminal in Python
Advertisements