Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am new to Javascript so I apologize if I am using the wrong terms. I have a code where I need to show some HTML images, stored as variables containing multiple items in specific positions (defined by the class) such as:

 var fruit = ["<img class='item1' src='" + Banana + "'>" + 
        "<img class='item2' src='" + Apple + "'>" + 
        "<img class='item3' src='" + Grapes + "'>" + 
        "<img class='item4' src='" + Banana + "'>" +
        "<img class='item5' src='" + Grapes + "'>" + 
        "<img class='item6' src='" + Pear + "'>"],

Later, I need to use all of these items in a bigger picture called stimulus, e.g.:

stimulus: "<div class='container'> <img class='background' src='" + Background + "'>" + fruit + "</div>",

But then I need to only show 3 random fruits among the 6 listed in fruit. For instance, I would need to get something like:

stimulus: "<div class='container'> <img class='background' src='" + Background + "'>" + half_fruit + "</div>",

Where: var half_fruit = ["<img class='item1' src='" + Banana + "'>" + "<img class='item3' src='" + Grapes + "'>" + "<img class='item6' src='" + Pear + "'>"],

Is there any way I can do this, even if my images are not technically items of an array? I have specific fruit combinations (not just the one in the example, roughly 30 in total) and I need to remove 3 items of the ones in that specific combination while also leaving the others in the same position as they were before. This is why I am not storing images as strings in an array. Those variables are defined as var Banana = gorilla.stimuliURL('Banana.png'); etc. (Gorilla is a platform for creating experiments; the stimuliURL function calls image addresses from an online repository). Thank you for your time!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
166 views
Welcome To Ask or Share your Answers For Others

1 Answer

Thanks to your comments, I rearranged the variables as follows. First, I created variables for each image to access their addresses from Gorilla, e.g.

var banana_img = gorilla.stimuliURL('banana.png');

Then, I created variables for each fruit in each position:

var banana = ["<img class='item1' src='" + banana_img + "'>", "<img class='item2' src='" + banana_img+ "'>", "<img class='item3' src='" + banana_img+ "'>", "<img class='item4' src='" + banana_img + "'>", "<img class='item5' src='" + banana_img + "'>", "<img class='item6' src='" + banana_img + "'>"];

Then, I saved each fruit combination as a variable, e.g.

var fruit = [banana[0], apple[1], grapes[2], banana[3], grapes[4], pear[5]]

FInally, I sorted it and selected the first 3 to get only three random items:

half_fruit = fruit.sort( function() { return 0.5 - Math.random() } ).slice(0, 3).join(" ");

Answer or comment below if you find a better way to do this!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...