Quotes help make search much faster. Example: "Practice Makes Perfect"

Thursday, December 20, 2012

LearnStreet Sample Solution Sudoku Solver Project Method 5



function isCorrectRow(sudoku, row) {
    // This method should return true if all the numbers from 1 to 9 are
    // present in the row indexed by the 'row' argument in the 'sudoku'
    // puzzle. It should return false if the row is incomplete or has duplicates.
    // Note that the "empty" rows will actually contain the value 0.
    // 1) Start off by making a *copy* of the row.
    // 2) Then sort the new array that you have made so that the
    // numbers run in ascending order.
    // 3) You should then be able to run through this sorted array and
    // easily determine if all the required numbers are there.
    // REPLACE THIS CODE WITH YOUR isCorrectRow() METHOD
    var returnValue = true;
    var rowCopy = [];
    for(var i=0; i<9; i++){
        rowCopy[i] = sudoku[row][i];
    }
    rowCopy.sort();
    for(var i=1; i<=9; i++){
        if(rowCopy[i-1]!==i){
            returnValue = false;
        }
    }
    return returnValue;
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts