2

I'm trying to count the number of occupied spaces, then return that count from #turn_count. Right now I'm returning the orig array.

I'm also counting every iteration, instead of occupied indices.

board = [" ", " x", " ", " ", "O", "O", " ", " ", "X"]


def turn_count(board)
  counter = 0
board.each do |index|

  if index = "x" || index = "o"
    counter += 1
    puts "#{counter}"
  end
end

end

turn_count(board)
6
  • 1
    what's your question exactly? Commented Feb 23, 2017 at 21:19
  • Simply return counter at the end of your method? Commented Feb 23, 2017 at 21:20
  • @ndn, when I return counter, I get 9, but I want 4. Commented Feb 23, 2017 at 21:24
  • @dax, I want to count only the occupied board[index], skipping the empty ones, then return 4, which is the number of occupied indices (with x's and o's). Commented Feb 23, 2017 at 21:25
  • @ndn I'm getting the picture. == Commented Feb 23, 2017 at 21:28

4 Answers 4

1

You just need to return the value of counter at the end of your function and also change your = to ==.

= is for assignment. == is for comparison.

def turn_count(board)
  counter = 0

  board.each do |turn|
    if turn.downcase == "x" || turn.downcase == "o"
      counter += 1
      puts "#{counter}"
    end
  end

  counter
end

I've also added the downcase method so that you're comparison is consistent. Otherwise, if you're looking for x but the array contains an X, you won't get a match.


SIDENOTE:

I changed index to turn because what you're declaring for that each loop is not actually an index. It's the array element itself.

If you wanted to use the index in the loop, you'd need to do something like:

board.each_with_index do |turn, index|

SIDENOTE #2:

You could also do the each loop as a super clean one-liner:

board.each { |t| counter +=1 if ['x', 'o'].include?(t.downcase) }

Just another option. :-)

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

2 Comments

You actually pointed out the problems with my code, ==, capital vs. lowercase. These are things that I must pay attention to. You have really helped me get unstuck. Thanks!
No problem at all. Welcome to Ruby! You'll love it once you get more acquainted with it. It's awesome.
1

I'm trying to count the number of occupied spaces

board = [" ", " x", " ", " ", "O", "O", " ", " ", "X"]
board.count { |s| s != " " } #=> 4

2 Comments

Nice. In Rails, it would be even shorter : board.count(&:present?)
that give 5, not 4, due to the one entry with multiple spaces
1
[" ", " x", "    ", " ", "O", "1O", "    ", " ", "X"].count { |s| s =~ /\S/ }
  #=> 4

You can do conditional counting for an array by passing a block to count that returns true for the elements you want to count.

In the above example the element is tested against a regex looking for anything other than whitespace in the field

1 Comment

@IanKenney, thanks for the edit. You may have noticed that I rolled back to my original answer. That was due to mixup on my part, relating to an answer I gave to a different question. I put you comment back when I saw my mistake. Good edit.
1

If i understand your question correctly, i guess you want to return the count of occupied values. To do this in ruby helpers are available. Like in your condition:

array = [" ", " x", " ", " ", "O", "O", " ", " ", "X"]

array.reject(&:blank?).count It will return a count 4.

So here, reject will skip all the blank spaces and give a count of those elements are present.

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.