Performed test task.

From comments:

Need to make a game. Field 4x4 square. For squares, the default state is “closed”. Clicking on the square switches the state to "open". You need to open 2 squares. Every second square is the same color. Clicked on two squares of the same color, they go into the "open" state. If with a different color, then back to the "closed". It is necessary to open all the squares.

Here is my attempt:

var square0 = document.getElementById("0"); var square1 = document.getElementById("1"); var square2 = document.getElementById("2"); var square3 = document.getElementById("3"); var square4 = document.getElementById("4"); var square5 = document.getElementById("5"); var square6 = document.getElementById("6"); var square7 = document.getElementById("7"); var square8 = document.getElementById("8"); var square9 = document.getElementById("9"); var square10 = document.getElementById("10"); var square11 = document.getElementById("11"); var square12 = document.getElementById("12"); var square13 = document.getElementById("13"); var square14 = document.getElementById("14"); var square15 = document.getElementById("15"); var img0 = document.getElementById("img0"); var img1 = document.getElementById("img1"); var img2 = document.getElementById("img2"); var img3 = document.getElementById("img3"); var img4 = document.getElementById("img4"); var img5 = document.getElementById("img5"); var img6 = document.getElementById("img6"); var img7 = document.getElementById("img7"); var img8 = document.getElementById("img8"); var img9 = document.getElementById("img9"); var img10 = document.getElementById("img10"); var img11 = document.getElementById("img11"); var img12 = document.getElementById("img12"); var img13 = document.getElementById("img13"); var img14 = document.getElementById("img14"); var img15 = document.getElementById("img15"); var count = 0; var arr = []; 

The reviewers did not like this code. They said, so just do not have to do.

How can you do otherwise? You need to access the <div> and <img> elements.

Closed due to the fact that the essence of the question is not clear to the participants of the VenZell , D-side , Alexey Shimansky , cheops , Kromster 16 Jun '16 at 7:03 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    State the essence of the task that you have solved. While your question is meaningless in terms of code inspection. - VenZell
  • How to make this code better? - Roman Pawliw
  • 2
    I repeat, state the essence of the task before you. Without this, the question is meaningless. - VenZell
  • one
    @RomanPawliw you now what is the translator write? I would refer to the tundra for such wording of thoughts - Aleksey Shimansky
  • one
    @ Alexey Shimansky, as I understand it, talking about the game "Find a pair." - VenZell

2 answers 2

Note: The element's id attribute can start with a digit, but for the sake of clarity, it's better to use something like "square" + цифра for id .

You can put everything in the dictionary first ( squares and images )

 var squares = {}; var numOfSquares = 15; for (var i = 0; i < numOfSquares; i++) { squares["square" + i.toString()] = document.getElementById("square" + i.toString()); } var images = {}; var numOfImages = 15; for (var i = 0; i < numOfSquares; i++) { images["img" + i.toString()] = document.getElementById("img" + i.toString()); } 

Then, you can get and use elements as follows (example: square1 and img6 )

 var square1 = squares["square1"]; var img6 = images["img6"]; 
  • Specify in your answer that the element id attribute cannot begin with a digit. - VenZell
  • 3
    @VenZell, outdated requirement: placed by ID of values. - Grundy
  • @Grundy, fig ce, did not know. Thank. - VenZell
  • @VenZell, to be honest, I didn’t know either :-) I wanted to support the words with a link, I looked at the link to the 4 standard, looked for a new one and here :) - Grundy

Perhaps it would be better not to loop getElementById, but use querySelectorAll ?

 var squares = document.querySelectorAll('div'); var imgs = document.querySelectorAll('img'); 
  • Bad idea, you can choose too much. You must at least select a class or role to add. - Pavel Mayorov
  • @PavelMayorov, or search in the container, and not in the whole document - Grundy
  • @Grundy, @PavelMayorov I did not offer a ready-made solution, but only showed a more convenient way (without cycles), because you won’t deny that querySelectorAll better than for + getElement(s)By* - Artem
  • @Artem, better by what criterion? :-) - Grundy
  • @Grundy (1) lack of a cycle, (2) part of the work with the collection / array goes to the shoulders of the native function - Artem