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

264
Views
Why is my hex code generator returning variable names instead of their values?

So I am trying to create a random hex code generator. So far I am just trying to get the 6 random values to be presented in the HTML.

    // Letters A-F can be used and numbers 0-9
var button = document.querySelector(".hex-btn");
var color = document.querySelector(".chosen-color");

button.addEventListener("click", function () {
  var number = Math.floor(Math.random() * 10);
  var letterString = ["a", "b", "c", "d", "e", "f"];
  var chosenLetter = Math.floor(Math.random() * 6);
  var letter = letterString[chosenLetter];

  hex1 = hexOptions();
  hex2 = hexOptions();
  hex3 = hexOptions();
  hex4 = hexOptions();
  hex5 = hexOptions();
  hex6 = hexOptions();

  color.innerHTML = "#" + hex1 + hex2 + hex3 + hex4 + hex5 + hex6;
});

//So each hex value can be number or letter 
function hexOptions() {
  var option = ["number", "letter"];
  var randomOption = Math.floor(Math.random() * 2);
  return option[randomOption];
}

Why is it returning the names of the variables rather than their values? Ex. #numbernumberletternumberletternumber

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

0

Your hexOptions function returns the string of either 'number' or 'letter' You then assign the variables hex1 through to hex6 as the result of this function, concatenate them and return.

What you probably want is to use the result of hexOptions to choose either a letter or a number. Something like:

button.addEventListener("click", function () {
  var number = Math.floor(Math.random() * 10);
  var letterString = ["a", "b", "c", "d", "e", "f"];
  var chosenLetter = Math.floor(Math.random() * 6);
  var letter = letterString[chosenLetter];

  hex1 = hexOptions();
  const hex1Value = hex1 === 'number' ? Math.random() * 10 : letter;
  // repeat for each of your hex values

  color.innerHTML = "#" + hex1Value + hex2Value + hex3Value + hex4Value + hex5Value + hex6Value;
});
about 4 years ago · Juan Pablo Isaza Report

0

Your question above is why you get the result that you're getting. I will be answering exactly that.

var option = ["number", "letter"];

This is an array that contains 2 strings. Surely you're trying to do something else with that.

Calling option[0] on the above would return the word "number". That's the reason you're getting that result.

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!