Sudoku Solve Function

Hello, I didn’t want to research this specific function too deeply in case I spoil some things for myself but I’m having a little trouble with the solve function inside sudoku-solver.js.

My current idea is to loop through each number/period of the string, if it is a period it will loop through 1-9 and check if they pass other functions (the row, column, region functions) and then insert the number in.
To do that I am using slice(0, index) plus character plus slice(index +1).
But the output keeps shortening the original after a few loops.

if puzzleString= …9……2432…1…69.83.9…6.62.71…9…1945……6…

solve(puzzleString) {
    const rows = "ABCDEFGHI";
    let count = 0;
    var solution = puzzleString
    while(count <= 8){
      for(var k in puzzleString){
        if(count == 8 && k == 0){
        else if(k%9 == 0 && k != 0){
        let column = (k%9) ;
        let roww = rows[count];
        let num = puzzleString[k];
        let exp = 1;

        function makeChange(str, index, letter){
          let s1 = str.slice(0, index)
          let s2 = str.slice(index+1)
          console.log(" ")
          console.log("s1 is " + s1)
          console.log("s2 is " + s2)
          console.log( " ")
          return s1 + letter + s2;

        if(num == "."){
          while(exp <=9){
            if(this.checkRowPlacement(solution, roww, column+1, exp)){
              console.log(roww + (column+1) + " is a period and " + exp + " can fit here.")
              console.log("k equals " + k)
            solution = makeChange(solution, k, exp);
            exp = 1;
              console.log("   ")
            } else {

Output would be
at the end.
On console I can see that it keeps shortening every loop. Not sure why.

edit: I am only checking against the checkRowPlacement for now until I figure out this problem.

I see this will get buried. Nothing for it but to press on. I’ll try to update this thread if I find a solution for future people.

According to the code that you posted, the makeChange function works as intended, so it is possible that something elsewhere in your code is causing interference. Could you post a link to your complete code?

As an aside, you may find it easier to convert the string into a two-dimensional array and work on that, then convert it back into a string when you need to return it to the client.

Ended up doing the string → array → string method and for some reason that works great.
Thanks for the reply!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.