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;
i == n - j + 1PS: try used to starting iteration from0, since almost everything around you in programming is 0-based