I am new to JavaScript, and I am struggling with this one question from class. It is probably something easy, but I am completely stuck at the moment.
Anyway, here is the problem:
I have to create a table of alternating characters of x
and o
based on a user-specified number of rows and columns. For instance, if the user wanted 3 rows and 3 columns, it would have to look like this:
xox
oxo
xox
I am completely lost on how you can create an alternating value in an array. This is what I have so far (below), but the logic of this is completely wrong. If anyone can give me some advice that would be great! I have been looking at this problem for days, but just can’t seem to piece it together.
// a = user input # of columns
// b = user input # of rows
function firstTest(a,b) {
var firstArray = [];
var total = [];
for (i = 0; i < a; i+=1) {
firstArray.push("xo");
}
for (i=0; i<b; i+=1){
total.push(firstArray);
}
return(total);
}