Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of using <code>splice</code> or modifying the original array, why not just store the index of the randomly-selected item into a new array (after making sure it wasn't already selected)?</p> <p>Also, instead of having a container for each image, why not just let them flow one after the other (of course I don't really know what you are ultimately trying to do)?</p> <p>Here is a function that demonstrates this. <a href="http://jsfiddle.net/ErikE/zN93u/" rel="nofollow noreferrer">See this in action in a JS Fiddle</a> or <a href="http://jsfiddle.net/ErikE/zN93u/embedded/result/" rel="nofollow noreferrer">interact with the full-screen result</a>.</p> <p>(My apologies for using jQuery if you don't want to do that; it was simply easier for me to get you a working demo by using a library.)</p> <pre><code>function imageReset() { var selected, img, imageContainer = $('#images').empty(); currentImages = []; while (currentImages.length &lt; 3) { selected = Math.floor(Math.random() * items.length); if (currentImages.indexOf(selected) === -1) { currentImages.push(selected); } } selectedImage = currentImages[Math.floor(Math.random() * 3)]; $('#word').empty().html(items[selectedImage].name); $.each(currentImages, function(i, imageIndex) { img = $('&lt;img&gt;').attr({ src: items[imageIndex].image, width: "200", height: "200" }); if (imageIndex === selectedImage) { img.data('correct', true); } img.appendTo(imageContainer).on('click', imageClick); }); } </code></pre> <p>Here's a preview of what the final page looks like:</p> <p><a href="http://jsfiddle.net/ErikE/zN93u/embedded/result/" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Cz5lh.png" alt="Image Recognizer JS Fiddle"></a></p> <p>Notes about not using a library:</p> <ul> <li>The trickiest part will be determining if the correct image was clicked on. You can do that using an expando attribute such as "data-correct", but this may be hard to do cross-browser. I used jQuery's data storage, ala <code>data()</code>. You could also, instead of marking the image specially as the selected one, check the 'src' of the image itself and then using this, loop through the <code>currentImages</code> array to find which original item it is and whether it is the right one.</li> <li><code>$.each()</code> loops through an array. You can do fine with <code>for (i = 0, l = arr.length; i &lt; l; i += 1) {}</code>.</li> <li><code>empty()</code> removes all the contents of an element. You can simply delete the first child until there are no children left.</li> <li><code>$('&lt;img&gt;')</code> does the same thing as <code>createElement('img')</code>.</li> </ul> <p>I highly recommend using a library. I cannot see a single reason to avoid one besides "I just want to". You can still learn the plain javascript methods even when using a library, by studying the underlying code. Frankly, if I were a software development business owner, I would <em>require</em> my junior javascript programmers to use the library because the coding speed and quality would be so much higher. They could and maybe should eventually learn how to do it the "old fashioned" way, but in the meantime I would not want them to waste time.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload