Created
May 24, 2012 13:53
-
-
Save igaiga/2781668 to your computer and use it in GitHub Desktop.
Arrayオブジェクトの要素が奇数の項目を全て足すコードを書いてください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Arrayオブジェクトの要素が奇数の項目を全て足すコードを書いてください。 | |
# 例えば array = [2,3,5,7,11] の場合、 3+5+7+11 = 26 が表示されればOKです。 | |
# ヒント:奇数かどうか調べるのは Fixnum#odd? メソッド | |
# 1.odd? → true, 2.odd? → false | |
# ちなみに偶数か調べるのは Fixnum#even? メソッドです。 | |
array = [2,3,5,7,11] | |
sum = 0 | |
array.each do |i| | |
sum += i if i.odd? | |
end | |
p sum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment