- 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
Randomize color by number in JavaScript
We are required to write a function that returns a random hex color. So here is the code for doing so −
Example
const generateRandomColor = () => { const keys = '0123456789ABCDEF'; let color = ''; while(color.length < 6){ const random = Math.floor(Math.random() * 16); color += keys[random]; }; return `#${color}`; }; console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor()); console.log(generateRandomColor());
Output
The output in the console will be −
#C83343 #D9AAF3 #9D55CC #28AE22
Advertisements