javascript - How to alternate values in an array? -
i new javascript, , struggling 1 question class. easy, stuck @ moment.
anyway, here problem:
i have create table of alternating characters of x
, o
based on user-specified number of rows , columns. instance, if user wanted 3 rows , 3 columns, have this:
xox oxo xox
i lost on how can create alternating value in array. have far (below), logic of wrong. if can give me advice great! have been looking @ problem days, can’t seem piece together.
// = user input # of columns // b = user input # of rows function firsttest(a,b) { var firstarray = []; var total = []; (i = 0; < a; i+=1) { firstarray.push("xo"); } (i=0; i<b; i+=1){ total.push(firstarray); } return(total); }
you need check if sum of value of row , value of column odd or even:
function firsttest(a,b) { table = []; ( x = 1 ; x <= ; x++ ) { row = []; ( y = 1 ; y <= b ; y++ ) { if (((x+y) % 2) == 0) { row.push('x'); } else { row.push('o'); } } table.push(row); } return table; }
Comments
Post a Comment