-2

I want to create a random color-picker with javascript and for that, I want random hex codes. The part with getting the hex codes is done but they appear as strings on different lines when I use console.log(); and not as one string on one line.

Does someone know how to change it? I already tried to set it as an array but it still appears on different lines because I used a for-loop and I can't change the name of the different strings because of that. Sorry for my poor explanation but here is the code for you to run it and maybe help me out:

let hexDigits = ["A", "B", "C", "D", "E", "F",
  "1", "2", "3", "4", "5", "6", "7", "8", "9"
];

for (let i = 0; i < 6; i++) {
  let hexColor = hexDigits[Math.floor(Math.random() * hexDigits.length)];

  console.log(hexColor);
}
Andy
  • 61,948
  • 13
  • 68
  • 95
  • 3
    Why do you re-invent the wheel? Here use this https://stackoverflow.com/questions/5092808/how-do-i-randomly-generate-html-hex-color-codes-using-javascript – bill.gates May 04 '22 at 21:47
  • @NinoFiliu The Q&A you refer to as a duplicate is itself a duplicate – Dexygen May 04 '22 at 21:51

1 Answers1

0

Store to a string and append to it, printing only when you're done.

let hexDigits = ["A", "B", "C", "D", "E", "F",
  "1", "2", "3", "4", "5", "6", "7", "8", "9"
];

let hexColor = "";

for (let i = 0; i < 6; i++) {
  hexColor += hexDigits[Math.floor(Math.random() * hexDigits.length)];
}

console.log("#" + hexColor);
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
  • Thank you! I just started learning javascript and I sat there for an hour and tried everything and the result was that close :D – MeinPingwar1 May 04 '22 at 21:51