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

113
Views
Isolating a part of a string

well... for begining, i m building some dumb and easy projects because i'm starting learning the world of front end development. One of those you can find in internet was this Color picker idea. Some page where with a button you can generate a random color. all random, no connections between the colors o something else.

(i'm not a native english speaker so sorry if i write something and you dont get it right)

Here's the link to the repository

so...

I build a function that makes a random HSL color randomizing the values and then building a string and puting that in the css

function getRandomHsl(){
let hueValue = 0;
hueValue = Math.floor(
    Math.random() * (360 + 1)
)

let satValue = 0;
satValue = Math.floor(
    Math.random() * (100 + 1)
)

let lightValue = 0;
lightValue = Math.floor(
    Math.random() * (100 + 1)
)

return 'hsl(' + hueValue + ', ' + satValue + '%, ' + lightValue + '%)';}

if you can give me your opinion on that function and tell me if you would have done it inanother way.

so... i wanted to find the way to if the Lightness would have a low value the color of the font change it to some white or something like that. And this is what i figured out. (the first part of the code is the button changing the value of the background color of the main div. This works well. The problem comes after).

hslBtn.addEventListener('click', () => {
 let hslStringComplete = getRandomHsl();
 colorShowcase.textContent = hslStringComplete;
 document.getElementById('color-container').style.backgroundColor = hslStringComplete;

/*================================= change the color font od the text when its to dark */

let hslLightValue = hslStringComplete;
let lightValue = hslLightValue.toString().substr(13,3).replace(/\D/g,'')
console.log(hslLightValue.substr(13,3).replace(/\D/g,''));
    
if(lightValue < 40){
    innerHTML('input').style.color = "white";
}})

i have really strougle it out coming up with this idea so i want some opinion on how could i have done in other way.

PD: `if(lightValue < 40){ innerHTML('input').style.color = "white";}

this is hte HTML: <input class="background-container"><h3 class="forWhiteColor">Background Color : <span class="color-showcase">#messi</span></h3></input>

this part get me an error and the html doesn get changes. Here's the error.

console error

thanks in advance.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

innerHTML('input') should probably be document.getElementById('color-container') instead.

also you should note that when you extract a part of a string, you get a string. And comparing a string to a number won't work well, you first need to convert your string to a number with parseInt (for integers) or parseFloat (for float)

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!