Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="grid">

</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Shojumaru');

* {
	box-sizing: border-box;	
}

.grid {
	margin: 0 auto;
	width: 100vw;
	height: 100vw;
	max-width: 900px;
	max-height: 900px;
	display: flex;
	flex-wrap: wrap;
  font-size: 30px;
	font-family: 'Shojumaru', cursive;
}

.cell {
	width: calc(100% / 9 );
	height: calc(100% / 9 );
	border: 1px solid black;
	flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-items: center;
	position: relative;
	background: hsl(199, 62%, 69%);
}


.cell:nth-child(n+4):nth-child(-n+6), 
.cell:nth-child(n+13):nth-child(-n+15), 
.cell:nth-child(n+22):nth-child(-n+24),

.cell:nth-child(n+28):nth-child(-n+30), 
.cell:nth-child(n+37):nth-child(-n+39), 
.cell:nth-child(n+46):nth-child(-n+48),

.cell:nth-child(n+34):nth-child(-n+36), 
.cell:nth-child(n+43):nth-child(-n+45), 
.cell:nth-child(n+52):nth-child(-n+54), 

.cell:nth-child(n+58):nth-child(-n+60), 
.cell:nth-child(n+67):nth-child(-n+69), 
.cell:nth-child(n+76):nth-child(-n+78){
	background: hsl(80, 14%, 86%);
}

.highlight.highlight.highlight {background-color: hsl(360, 94%, 94%);}
              
            
!

JS

              
                const grid = document.querySelector('.grid')
let cells
const board = Array.from({length: 81})
const usedNumbers = []

// fill usedNumbers with empty sets to keep track of numbers allready tried for a cell
function populateValues() {
  for (let i = 0; i < 81; i++) {
    usedNumbers[i] = new Set()
  }
}

// helper function returns the row in which the cell is (1-9)
function returnRow(index) {
  return ~~(index / 9) + 1
}

// helper function returns the column in which the cell is (1-9)
function returnCol(index) {
  return ~~(index % 9) + 1
}

// returns array with all peers in the same row
function checkRow(index) {
  const rowNum = returnRow(index)
  const row = board.slice((rowNum - 1) * 9, rowNum * 9)
  return row
}

// returns array with all peers in the same column
function checkCol(index) {
  const colNum = returnCol(index)
  const col = board.filter((e, i, a) => (~~(i % 9) + 1) == colNum ? true : false)
  return col
}

// returns array with all peers in the same block (3x3)
function checkBlock(index) {
  const firstCol = ~~((returnCol(index) - 1) / 3) * 3 + 1
  const firstRow = ~~((returnRow(index) - 1) / 3) * 3 + 1

  const block = []

  for (let i = 0; i < 3; i++) {
    const firstCell = board[(((firstRow - 1 + i) * 9) + (firstCol - 1))]
    const secondCell = board[(((firstRow - 1 + i) * 9) + (firstCol))]
    const thirdCell = board[(((firstRow - 1 + i) * 9) + (firstCol + 1))]
    block.push(firstCell)
    block.push(secondCell)
    block.push(thirdCell)
  }
  return block
}

// first we need to get all invalid numbers, i.e. numbers allready in use by peers
function getInvalidNumbers(cell) {
  const peers = new Set([...checkRow(cell), ...checkCol(cell), ...checkBlock(cell), ...Array.from(usedNumbers[cell])])
  peers.delete(undefined)
  return peers
}

// then we need have to 'invert' the invalid numbers array to get the valid numbers
function getValidNumbers(cell) {
  const invalidNumbers = getInvalidNumbers(cell)
  const validNumbers = []
  for (let i = 1; i <= 9; i++) {
    if (!invalidNumbers.has(i)) {
      validNumbers.push(i)
    }
  }
  return validNumbers
}

// main function to fill the board with numbers and to initialize backtracking if necessary
function fillBoard(boardArray) {
  let i = 0
  while (i < 81) {
    let validNumbers = getValidNumbers(i)
    let number = validNumbers[~~(Math.random() * validNumbers.length)]
    if (number) {
      board[i] = number
      usedNumbers[i].add(number)
      i++
    } else {
      board[i] = undefined
      usedNumbers[i].clear()
      i--
    }
  }
}

// render the board to the screen
function fillGrid(array, htmlElement) {
  array.forEach(e => {
    htmlElement.insertAdjacentHTML('beforeend', `<div class="cell _${e}">${e}</div>`)
  })
  cells = document.querySelectorAll('.cell')
  cells.forEach(cell => {
    cell.addEventListener('mouseover', highlightCells, {capture: false})
    cell.addEventListener('mouseleave', highlightCells, {capture: false})
  })
}

// highlights all cells with the same number in it
function highlightCells(e) {
  const hoveredCells = document.querySelectorAll(`.${e.target.classList[1]}`)
  hoveredCells.forEach(cell => cell.classList.toggle('highlight'))
}

populateValues()
fillBoard(board)
fillGrid(board, grid)
              
            
!
999px

Console