OneCompiler

Help

Im writeing JavaScript and I can't seen to figure out how to add random variables.
the code im using is

console.log(Math.floor(Math.random() * 255));
console.log(Math.floor(Math.random() * 255));
console.log(Math.floor(Math.random() * 255));
console.log(Math.random() * 1);

i made a random RGBA maker but im just curious how to add the random numbers

1 Answer

3 years ago by

Try this:

var RGBA = {
  "R": Math.floor(Math.random() * 255),
  "G": Math.floor(Math.random() * 255),
  "B": Math.floor(Math.random() * 255),
  "A": Math.floor(Math.random() * 1),
};
console.log("R: " + RGBA["R"]);
console.log("G: " + RGBA["G"]);
console.log("B: " + RGBA["B"]);
console.log("A: " + RGBA["A"]);
 

This uses an object to store the values of "R", "G", "B", and "A". If you wanted to add together the values (for whatever reason it may be), try adding this to the end of that:

var RGBAtotal = RGBA["R"] + RGBA["G"] + RGBA["B"] + RGBA["A"]
console.log(RGBAtotal)
3 years ago by ilovapples