0

Good afternoon.

I'm trying to make an algorithm outputs a 2d array of number, working towards a sudoku solver. I'm trying to use a for loop to create a 2d array and add a number of arrays. Anyway when i was testing I noticed if I log the i, I don't get 1,2,3,etc. I either get 0 or 9(example). I'l post the full code. Hopefully that all makes sense and thanks!

const output = document.querySelector("div");

function randomNumber(range){
    //generates a number
   return Math.ceil(Math.random()*range);
}

function randomLine(range){
    //generates a random line without repeating numbers
    let arr = [];
    for (i=0;i<range;){
        let num = randomNumber(range);
        if (checkMatch(num,arr) == false){
            arr.push(num);
            i++;
        }
    }
    return arr;
}

function checkMatch(num,arr){
    //checks a single number against an array
    let match = false;
    let len = arr.length;
    for (i=0;i<len;i++){
        if (arr[i] == num){
            match = true;
        }
    }
    return match;
}

function htmlOutput(range){
    //The problem
    for (i=0;i<range;i++){
        console.log(i);
        console.log(randomLine(range));
        console.log(i);
    }
}

htmlOutput(9);
Adam Shaw
  • 11
  • 2

0 Answers0