Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys i dont seem to know whats the problem here,

never encountered before, i get allocation size error in the 
console, and the IDE telling me the string too long too,

second question is how do i apply this shuffle function into the code?

       var gNums = []
    var gInterval = null
    var gBoard = creatBoard()
    
    
    renderBoard()
    function renderBoard() {
        strHtml = ''
        for (var i = 0; i < gBoard.length; i++) {
            var row = gBoard[i]
            strHtml += '<tr>'
            for (var j = 0; j < row.length; i++) {
                cellvalue = row[j].value
                var className = (row[j].isClicked) ? 'clicked' : ''
                strHtml += `
                <td class="${className}" onclick="numClicked(this)">
                ${cellvalue}
                </td> `
    
            }
            strHtml += `</tr>`
        }
        var ElBoard = document.querySelector('.board')
        ElBoard.innerHTML = str.strHtml
    
    }
    
    
    
    function creatBoard(difficulty = 16) {
        resetNums()
        var board = []
        for (var i = 0; i < Math.sqrt(difficulty); i++) {
            board[i] = []
            for (var j = 0; j < Math.sqrt(difficulty); j++) {
                board[i][j] = creatCell()
            }
    
        } return board
    
    }
    
    
    
    function creatCell() {
        var num = gNums.splice(gNums.length - 1, 1)
        return {
            value: Number(num),
            isClicked: false
        }
    }
    
    
    
    
    
    resetNums()
    function resetNums(difficulty = 16) {
        gNums = []
        for (var i = 0; i <= difficulty; i++) {
            gNums.push(i)
        }
        return gNums
    }
    
    
    
    
    function shuffle(array) {
        var m = array.length, t, i;
        while (m) {
            i = Math.floor(Math.random() * m--);
            t = array[m];
            array[m] = array[i];
            array[i] = t;
        }
    
        return array;
    }


What I have tried:

i tried breaking the string up with spaces for more readebility and finding the error, looks fine tho
Posted
Updated 18-Jan-20 2:17am

1 solution

This Stack Overflow question[^] presents a very similar case to yours. See the first answer there -- concatenating string in a loop is not a good idea. It's better to keep an array, push all string parts into that array, and use .join after the loop to concatenate it all together.
 
Share this answer
 
Comments
Member 14719507 18-Jan-20 8:40am    
can u show me a solution on this code? looked at the answer and i cant find a way to fix mine
i basically need to keep the entire string literal on the top of the function outside the loops and creat an empty array and push that string template into it?
however the variables inside the template do not change if its ouside the loop ,they remain static.
also an answer to the second question would be more than welcome mate :D
Thomas Daniels 18-Jan-20 8:48am    
now you have strHtml = '' and strHtml += something. Replace that first by strParts = [] and the second with strParts.push(something). After the loop, you can do strHtml = strParts.join("");

For the second question, I don't know what you mean. You can just do shuffle(yourArray);.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900