LoginSignup
0
0

More than 1 year has passed since last update.

Ruby 問題⑥ 特定の数字を検知するプログラムの実装 

Posted at

はじめに

とうとう明日プログラミングスクール卒業します涙

卒業しても学習はし続け、Qiitaアプトプットも欠かさず頑張ります!

本日はドリルのinclude?メソッドを復習します。

問題

以下の要件を満たすarray123メソッドを実装。

・配列内に1,2,3が全て入っている場合は、「True」と出力してください
・配列内に1,2,3の全てが入っていない場合は、「False」と出力してください

出力例
array123([1, 2, 2, 3, 2]) → True
array123([1, 2, 5, ]) → False
array123([1, 1, 2, 3, 4, 3]) → True

ヒント

include?メソッドを使用します。

公式リファレンス

解答

def array123(nums)
  if nums.include?(1) && nums.include?(2) && nums.include?(3)
    puts "True"
  else
    puts "False"
  end
end

array123([1, 2, 2, 3, 2])

仮引数のnumsには配列[1, 2, 2, 3, 2]が格納されます。

そして〜だった時、"True",〜だった時,"False"なのでifを使います。

 def array123(nums)
   if #条件式
    puts "True"
   else
    puts "False"
   end
 end

この条件式には「1があるとき」かつ、「2があるとき」かつ、「3があるとき」という記述をします。
そこで配列に対し、include?メソッドを呼び出します。
 

.include?()←引数に探索する数字

条件式は下記になります。

nums.include?(1) && nums.include?(2) && nums.include?(3)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0