gpt4 book ai didi

javascript - 计算数组中随机生成的元素?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:09:50 24 4
gpt4 key购买 nike

我有这样的问题:

i = [
'pic1.gif',
'pic2.gif'
];

我从这样的两张图片中生成了一个包含 n 张图片的随机序列:

a = function(n){
str = '<div>'

for(i = 0; i < n; i++){
str += '<img src="'+ /*randomElement of array i*/ +'">';
}

str += '<div>';

return str;
}

我现在要做的是如何统计生成的pic1个数?

最佳答案

如果你知道图片的名称,并且没有改变,我们可以使用简单的变量找到计数

a = function(n){
str = '<div>'
**//adding a temp variable**
count_img =0;
for(i = 0; i < n; i++){
str += '<img src="'+ /*randomElement of array i*/ +'">';
**//check the image name
if(imagename matches){
//increment the count
count_img = count_img +1;
}**
}

**console.log(count_img);**
str += '<div>';

return str;

关于javascript - 计算数组中随机生成的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51373098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com