简体   繁体   中英

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:

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?

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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