Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
How to get hexadecimal characters from short string for colorcode

I want hexadecimal color code from username string. for long user name it is working correctly but for short name its failing. For freddie_steven it returns feddee, but for alex_steven it returns only aeee which is very short. How to fix this.

How to get valid hexadecimal color code from username to set a thumbnail background different with username.

#user-thumbnail{
    width: 100px;
    height: 100px;
    border-radius: 100%;
}

<div id='user-thumbnail'></div>

var username = 'freddie_steven';
var colorCode = '';

for(var i = 0; i<username.length; i++){
    if(username.charCodeAt(i) >= 97 && username.charCodeAt(i) <= 102)
        colorCode += username.charAt(i)
}

document.getElementById('user-thumbnail').style.background = '#' +colorCode.substr(0, 6);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Pick random character if the character does not match !

var username = 'alex_steven';
var colorCode = '';

var validHex = [
  'a',
  'b',
  'c',
  'd',
  'e',
  'f'
];
var randomNumber = Math.floor(Math.random() * validHex.length);

for (var i = 0; i < username.length; i++) {
  if (username.charCodeAt(i) >= 97 && username.charCodeAt(i) <= 102) {
    colorCode += username.charAt(i)
  } else {
    colorCode += validHex[randomNumber]
  }
}

console.log(colorCode.substr(0, 6));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!