生成指定数量的随机十六进制和RGB颜色的功能
试图编写一个生成指定量的随机RGB或十六进制颜色的函数。它以“ RGB”或“ HEX”(类型),然后将“ N”(生成数量)作为参数,但是在运行代码时我会得到NAN。这是我写的:
function generateColors(type, n) {
let result = ''
var quantity = Number(n)
if (type === 'rgb') {
let num = Math.round(0xffffff *
Math.random());
let r = num >> 16
let g = num >> 8 & 255
let b = num & 255
return ('rgb(' + r + ', ' + g + ', ' + b + ')') * quantity;
} else if (type === 'hexa') {
let hexDigits = '0123456789ABCDEF'
let randomHex= []
for (var i = 0; i < 6; i++) {
randomHex +=
hexDigits.charAt(Math.floor(Math.random() *
hexDigits.length));
}
return [randomHex] * quantity;
} else {
console.log('type not applicable')
}
return result
}
console.log(generateColors('rgb', 3))
console.log(generateColors('hexa', 3))
不确定我缺少什么,或者如果我应该做一个Switch语句,但是欢迎任何建议。
Trying to write a function that generates a specified amount of random rgb or hex colors. It takes 'rgb' or 'hex'(type) and then 'n'(quantity to generate) as parameters, but I'm getting NaN when running code. Here's what I have written:
function generateColors(type, n) {
let result = ''
var quantity = Number(n)
if (type === 'rgb') {
let num = Math.round(0xffffff *
Math.random());
let r = num >> 16
let g = num >> 8 & 255
let b = num & 255
return ('rgb(' + r + ', ' + g + ', ' + b + ')') * quantity;
} else if (type === 'hexa') {
let hexDigits = '0123456789ABCDEF'
let randomHex= []
for (var i = 0; i < 6; i++) {
randomHex +=
hexDigits.charAt(Math.floor(Math.random() *
hexDigits.length));
}
return [randomHex] * quantity;
} else {
console.log('type not applicable')
}
return result
}
console.log(generateColors('rgb', 3))
console.log(generateColors('hexa', 3))
Not sure what I'm missing, or if I should do a switch statement instead, but any advice is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您将需要使用循环...
您可以在功能中循环n次。将输出推入结果并返回结果。它看起来像这样的东西:
工作示例> - 不要忘记打开控制台;)
you will need to use loop...
you can loop n times inside the function. push the output into the result and return the result. it will look like something like this:
Working example - dont forget to open the console ;)
或者,略短:
Or, slightly shorter:
GenerateColors函数采用两个参数,类型和NUM。如果类型为“ RGB”,它将通过创建数量的数字来生成随机的RGB颜色,并将它们存储在数组中。
十六进制变量包含用于RGB颜色的十六进制字符,并且定义了空数组颜色以存储生成的颜色。
使用嵌套循环,该函数会生成NUM随机RGB颜色。每种颜色表示为三个值的数组(例如,[r,g,b]),其中颜色值(r,g,b)在0和255之间随机选择。
生成的R,G和B值被添加到称为Newarr的临时阵列中。
使用JOIN()方法将Newarr数组连接在一起,并且所得的字符串存储在名为ToString的变量中。
最后,使用此tostring值,创建了FinalVal变量并格式化为“ RGB(R,G,B)”格式。
颜色数组中充满了最终视频值,该值表示生成的颜色,并且返回该数组。
如果类型为“十六进制”,则该功能会生成随机的十六进制颜色。它还生成NUM随机十六进制颜色,并将它们存储在颜色数组中。
颜色阵列被定义为存储生成的颜色,而十六进制变量包含用于六角形颜色的十六进制字符。
每种颜色都以“#”符号开头,由六个十六进制字符组成。颜色值是使用十六进制中随机选择的字符创建的,并添加到颜色数组中。
生成的颜色被添加到颜色阵列中,该数组在末尾返回。
代码的输出将是“ RGB(R,G,B)”格式中的一组随机生成的RGB颜色,或者取决于“ #ABCDEF”格式中的一组随机生成的十六进制颜色,具体取决于指定的类型。
The generateColors function takes two parameters, type and num. If the type is 'rgb', it generates random RGB colors by creating num number of them and stores them in an array.
The hexChars variable contains the hexadecimal characters used for RGB colors, and an empty array colors is defined to store the generated colors.
Using nested loops, the function generates num random RGB colors. Each color is represented as an array of three values (e.g., [r, g, b]), where the color values (r, g, b) are randomly selected between 0 and 255.
The generated r, g, and b values are added to a temporary array called newArr.
The newArr array is concatenated using the join() method, and the resulting string is stored in a variable named tostring.
Finally, using this tostring value, the finalVal variable is created and formatted to be in the "rgb(r, g, b)" format.
The colors array is filled with finalVal values, which represent the generated colors, and this array is returned.
If the type is 'hex', the function generates random hex colors. It also generates num random hex colors and stores them in the colors array.
The colors array is defined to store the generated colors, and the hexChars variable contains the hexadecimal characters used for hex colors.
Each color starts with the '#' symbol and consists of six hexadecimal characters. The color value is created using randomly selected characters from hexChars and is added to the colors array.
The generated colors are added to the colors array, which is returned at the end.
The output of the code will be either a set of randomly generated RGB colors in the "rgb(r, g, b)" format or a set of randomly generated hex colors in the "#abcdef" format, depending on the specified type.