jeudi 23 octobre 2014

Iterate through an array using each_with_index without a separate index variable


Vote count:

0




I am trying to figure out how I can refactor my code below so that I can iterate through the array without using the "index1" and "index2" variables to keep track of my progress.



board.each do |cell|
diag1 << cell[index1]
diag2 << cell[index2]
index1 += 1
index2 -= 1
end


I tried using .each_with_index (see below), but I'm not sure how to increment my values for "i".



board.each_with_index do |cell, i|
diag1 << cell[i += 1]
diag2 << cell[i -= 1]
end


The array "board" is an n*n array of a tic-tac-to game that is at least 3x3 in size or larger and the block of code is supposed to check to see if there is a diagonal match. Thanks in advance!





asked 5 mins ago







Iterate through an array using each_with_index without a separate index variable

Aucun commentaire:

Enregistrer un commentaire