0

I am trying to make a matrix which has an X of X's. The following code produces a diagonal of X's from the top left to the bottom right but not from the top right to the bottom left. I am unsure of where to even begin. Should another for loop be created with a new variable? Or is there something as simple as adding an else if statement for variable j? Any help will be greatly appreciated.

var nMatrix =  "";
var n = prompt ("enter a number");
n = parseInt(n);
for (var i = 1; i <= n; i++) {
    var row = "| ";
    for (var j = 1; j <= n; j++) {
        if (i == j)
            row += "x "; //top left to bottom right diagonally      
        else 
            row += Math.floor (9*Math.random()+1)+" ";
    }               
    row += "|\n";
    nMatrix += row;
}
document.getElementById ("matrix").innerText = nMatrix;
2
  • 1
    i == n - j + 1 PS: try used to starting iteration from 0, since almost everything around you in programming is 0-based Commented Jun 4, 2015 at 5:48
  • It should be: ((i+j-1)==n) Commented Jun 4, 2015 at 5:51

3 Answers 3

1

Maybe something along the lines of:

function grid (size) {
    var out = '';

    for (var row = 0; row < size; row++) {
        out += '| ';
        for (var col = 0; col < size; col++) {
            out += Math.random() < 0.9 ? '  ' : 'x ';
        }
        out += '|\n';
    }

    return out;
}

Output of grid(12):

 |     x                 x |
 |       x x     x         |
 | x                     x |
 |                         |
 |           x             |
 |                         |
 |     x                   |
 |               x         |
 |                       x |
 |                         |
 |                     x   |
 | x                       |
Sign up to request clarification or add additional context in comments.

Comments

1

Assume you have (n*n) matrix looks like this:

   1 2 3 ... n    j
1 |           |
2 |           |
3 |           |
: |           |
n |           |

i

You understand (i == j) is the key to pick following x's, so you coded that successfully. (by the way, i prefer j == i).

   1 2 3 ... n    j
1 |x          |
2 |  x        |
3 |    x      |
: |           |
n |          x|

i

So you need the find out the rule that pick following x's:

   1 2 3 ... n    j
1 |          x|
2 |        x  |
3 |           |
: |  x        |
n |x          |

i

How do you see the rule of x's? Isn't that for every x, i+j = n+1 holds?

so what you need to check is if (j == n + 1 - i). If that holds, you need to draw x.


One thing you maybe want to take consideration into is when (j == i) and (j == n + 1 - i) both holds: this is possible when n is odd. If you're using 'else if' correctly, no problem will occur. However it will be good for you to thinking about this.

Comments

0

How about:

function grid(size) {
    var out = "";
    for (var i = 0; i < size; i++) {
        for (var j = 0; j < size; j++) {
            out += (i == j || i == (size - j - 1)) ? "X" : " ";
        }
        out += "\n";
    }
    return out;
}

Output of grid(9):

X       X
 X     X 
  X   X  
   X X   
    X    
   X X   
  X   X  
 X     X 
X       X

Here's a jsfiddle: https://jsfiddle.net/6w36s1kb/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.