So I have a bunch of inputs (81). Some of those inputs have a value, and some do not. Values are always 1-9. When a user clicks a button, I want to save all 81 inputs into a string. If an input has no value, it should be a zero in the string. This is what I have so far.
let board = document.querySelectorAll('input')
let newBoard = 0;
for (let i = 0; i < board.length; i++) {
// if (i = 0 && board[i].value !== '') {
// newBoard = board[i]
// }
// if (i = 0) {
// newBoard = board[i]
// }
if (board[i].value === '') {
newBoard += 0
}
else {
newBoard += board[i].value
}
}
newBoard = newBoard.substring(1)
The substring is to remove the first zero, as if I try to reset newboard on the first loop to first input (see above for // code), page stops working and just hangs/catches/whatever you want to call it.
If the first input has a value, then it works perfect. The string has a length of 81 characters. If the first input has NO value, the string is never 81. For example, if the first 3 inputs are blank, length is 78. If the first 2 inputs are blank, length is 79.
When I remove the substring, then if the first input is blank, string length is 81. If first 2 inputs are blank, length is 80. And if the first input has a value, length is 82.
Can someone advise what is going on? I've tried console.logging multiple places and I still can't figure out why/whats going on.