lundi 30 novembre 2020

Why is this not returning a string, and instead returning NaN [closed]

I am trying to learn js by writing a program, and I came across this problem with my code. I am trying to use js to generate a random hex code, but my program is not returning a string, and instead, saying that it is undefined.

var hex = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]
var colorone = "#" + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length];
var colortwo = "#" + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length];
var colorthree = "#" + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length];
var colorfour = "#" + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length];
var colorfive = "#" + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length] + hex[Math.floor(Math.random) * hex.length];

Here is my code. I set it to console.log colorone - colorfive, but the result is #undefinedundefinedundefinedundefinedundefinedundefined. Does anybody know why this happens and ohw I can fix this?




Aucun commentaire:

Enregistrer un commentaire