There are a few typos in your function. The ifs are not comparing, they are assigning to seed.length. Also r, g and b are being defined globally. Here is the fix:
if
seed.length
r
g
b
function Colorize(seed = "AA") { if (seed.length === 0) { seed = "AA"; } else if (seed.length === 1) { seed += "A"; } const r = (seed.charCodeAt(0) * 9).toString(16).slice(-2); const g = (seed.charCodeAt(1) * 9).toString(16).slice(-2); const b = (seed.charCodeAt(seed.length - 1) * 9).toString(16).slice(-2); return "#" + r + g + b; }
And if you want to take it one step further:
const stringToColor = (seed = "") => (paddedSeed => `#${[ paddedSeed.charCodeAt(0), paddedSeed.charCodeAt(Math.floor(paddedSeed.length / 2)), paddedSeed.charCodeAt(paddedSeed.length - 1), ] .map(value => (value * 9).toString(16).slice(-2)) .join("")}`)(seed.padEnd(3, "A"));
Cheers!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There are a few typos in your function. The
ifs are not comparing, they are assigning toseed.length. Alsor,gandbare being defined globally. Here is the fix:And if you want to take it one step further:
Cheers!