在点击时如何保持数组在索引[0]处。

huangapple go评论99阅读模式
英文:

How to have array on click stay at index[0]

问题

I am trying to make an array onclick go down an index. However, every time I get to 0, it goes to the highest indexed element. I tried to make "X" item go down, but it doesn't seem to work.

The go down means as an [0, 1, 2, "X", 4], when clicked the new array would look like [0, 1, "X", 3, 4]. However, when "X" is at index 0 i.e [ "X", 1, 2, 3, 4] the item's position of the array should not change upon clicking.

英文:

I am trying to make an array onclick go down an index. However, every time I get to 0, it goes to the highest indexed element. I tried to make "X" item go down, but it doesn't seem to work.

The go down mean as an [0, 1, 2, "X", 4], when clicked the new array would look like [0, 1, "X", 3, 4]. However, when "X" is at index 0 i.e ["X", 1, 2, 3, 4] the item's position of the array should not change upon clicking

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. // array
  2. let player1gameboard2 = [&quot;X&quot;, 1, 2, 3, 4, 5, 6]
  3. function changePositionPlayer1Down(player1gameboard2) {
  4. // prints out array
  5. console.log(player1gameboard2)
  6. document.getElementById(&quot;player1_test&quot;).innerHTML = player1gameboard2
  7. // condition if array is less than 1. Normally goes to highest index. should just stay at lowest index
  8. if (player1gameboard2.indexOf(&quot;X&quot;) === 0) {
  9. player1gameboard2.splice(
  10. //future index of element
  11. 0,
  12. // original element
  13. 0,
  14. // how to get original element
  15. // [0] means it is an array
  16. // means 1 element
  17. player1gameboard2.splice(0, 1)[0]
  18. )
  19. // prints out new array
  20. document.getElementById(&quot;player1_test&quot;).innerHTML = player1gameboard2
  21. }
  22. }

<!-- language: lang-html -->

  1. &lt;!-- Array printed out on screen --&gt;
  2. &lt;p id=&quot;player1_test&quot;&gt;&lt;/p&gt;
  3. &lt;!-- every time button clicked, indexed array goes down one space --&gt;
  4. &lt;button onclick=&quot;changePositionPlayer1Down(player1gameboard2)&quot;&gt;Go Down&lt;/button&gt;

<!-- end snippet -->

答案1

得分: 1

The issue almost comes from if(player1gameboard2.indexOf("X") === 0), due to your requirement, it should be if(player1gameboard2.indexOf("X") !== 0).

Second, it's on the updated index of elements. The index of move element "X" will change on every update, not just 0.

  1. const moveElement = "X";
  2. let player1gameboard2 = [0, 1, 2, moveElement, 4, 5, 6]
  3. function changePositionPlayer1Down(arr) {
  4. console.log(arr)
  5. const fromIndex = arr.indexOf(moveElement);
  6. if (fromIndex === 0) {
  7. return;
  8. }
  9. // replace "X" at fromIndex - 1
  10. arr.splice(
  11. fromIndex - 1,
  12. 0,
  13. // cut ["X"] out of array and get "X"
  14. arr.splice(fromIndex, 1)[0] // same and could replace as "X"
  15. )
  16. // update value of the number has been shifted
  17. arr[fromIndex]++;
  18. document.getElementById("player1_test").innerHTML = player1gameboard2
  19. }
英文:

The issue almost comes from if(player1gameboard2.indexOf(&quot;X&quot;) === 0), due to your requirement, it should be if(player1gameboard2.indexOf(&quot;X&quot;) !== 0)

Second, it's on the updated index of elements. The index of move element &quot;X&quot; will change on every update, not just 0.

  1. const moveElement = &quot;X&quot;;
  2. let player1gameboard2 = [0, 1, 2, moveElement, 4, 5, 6]
  3. function changePositionPlayer1Down(arr) {
  4. console.log(arr)
  5. const fromIndex = arr.indexOf(moveElement);
  6. if (fromIndex === 0) {
  7. return;
  8. }
  9. // replace &quot;X&quot; at fromIndex - 1
  10. arr.splice(
  11. fromIndex - 1,
  12. 0,
  13. // cut [&quot;X&quot;] out of array and get &quot;X&quot;
  14. arr.splice(fromIndex, 1)[0] // same and could replace as &quot;X&quot;
  15. )
  16. // update value of the number has been shifted
  17. arr[fromIndex]++;
  18. document.getElementById(&quot;player1_test&quot;).innerHTML = player1gameboard2
  19. }

huangapple
  • 本文由 发表于 2023年4月17日 02:05:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定