closing prompt shouldn’t affect the document

  Kiến thức lập trình

I’m making etch-a-sketch game and I got everything down except if a user closes the prompt or enters an empty string (presses OK without entering anything) the grid should stay the same with same drawing instead of restarting.

function askSize() {
  container.innerHTML = '';
  gridSize = prompt('Enter the grid size (1-100):')
  
  
  if (gridSize <= 100) {
    createGrid(gridSize)
    displayGridSize.textContent = `Current grid size is ${gridSize} x ${gridSize}`
    para.appendChild(displayGridSize)
  } 
  else {
    alert('Please enter a valid value.')
    return askSize()
  }
}


function createGrid(gridSize) {
  for (let i = 0; i < (gridSize * gridSize); i++) {
    const cell = document.createElement('div');
    cell.style.flexBasis = 100 / gridSize + '%';
    container.appendChild(cell);
  }
} 

LEAVE A COMMENT