index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const inputDiv = document.getElementById("inputs");
  2. const tbody = document.getElementById("tbody");
  3. const blocks = [];
  4. let inputs = localStorage.getItem("inputs");
  5. if(inputs){
  6. inputs = inputs.split(",");
  7. }else{
  8. inputs = [];
  9. }
  10. for(let i = 0; i < 25; i++){
  11. const input = document.createElement("input");
  12. input.type = "text";
  13. if(inputs[i]) input.value = inputs[i];
  14. inputDiv.appendChild(input);
  15. }
  16. for(let i = 0; i < 5; i++){
  17. const tr = document.createElement("tr");
  18. for(let j = 0; j < 5; j++){
  19. const td = document.createElement("td");
  20. tr.appendChild(td);
  21. blocks.push(td);
  22. }
  23. tbody.appendChild(tr);
  24. }
  25. const shuffle = (arr)=>{
  26. for (let i = arr.length - 1; i > 0; i--) {
  27. const j = Math.floor(Math.random() * (i + 1));
  28. [arr[i], arr[j]] = [arr[j], arr[i]];
  29. }
  30. };
  31. document.getElementById("saveInputs").addEventListener("click", ()=>{
  32. const inputs = document.querySelectorAll("#inputs input");
  33. let inputString = "";
  34. for(let i = 0; i < inputs.length; i++){
  35. if(inputs[i].value === "") continue;
  36. inputString += `${inputs[i].value},`;
  37. }
  38. if(inputString[inputString.length-1] === ",") inputString = inputString.slice(0, -1);
  39. localStorage.setItem("inputs", inputString);
  40. });
  41. document.getElementById("start").addEventListener("click", ()=>{
  42. const inputElems = document.querySelectorAll("#inputs input");
  43. const inputs = [];
  44. for(let i = 0; i < inputElems.length; i++){
  45. inputs.push(inputElems[i].value);
  46. }
  47. shuffle(inputs);
  48. const tds = document.querySelectorAll("#tbody td");
  49. for(let i = 0; i < tds.length; i++){
  50. const p = document.createElement("p");
  51. p.textContent = inputs[i];
  52. if(tds[i].firstChild) tds[i].removeChild(tds[i].firstChild);
  53. tds[i].appendChild(p);
  54. }
  55. });
  56. for(let i = 0; i < blocks.length; i++){
  57. blocks[i].addEventListener("click", ()=>{
  58. let svg = blocks[i].querySelector("svg");
  59. if(svg){
  60. blocks[i].removeChild(svg);
  61. }else{
  62. let cross = document.getElementById("cross");
  63. cross = cross.cloneNode(true);
  64. cross.removeAttribute("id");
  65. cross.style.display = "block";
  66. blocks[i].appendChild(cross);
  67. }
  68. });
  69. }