简体   繁体   English

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

[英]count randomly generated element from an array?

I have a problem as such: 我有这样的问题:

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

I generate a random sequence of n pictures from the two pictures as such: 我从两个图片生成n个图片的随机序列,如下所示:

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

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

    str += '<div>';

    return str;
}

What I need to do now is how to count the number of pic1 generated? 我现在需要做的是如何计算生成的pic1的数量?

if you know the name of the pic, and if does not change, we can find the count using simple variable 如果您知道图片的名称,并且没有变化,我们可以使用简单变量查找计数

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;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM