Ruby Array Exercises: Check whether a given array of integers of length 2 contains a 4 or a 7
Ruby Array: Exercise-17 with Solution
Write a Ruby program to check whether a given array of integers of length 2 contains a 4 or a 7.
Ruby Code:
def check_array(nums)
if(nums[0] == 4 || nums[0] == 7)
return true
end
return (nums[1] == 4 || nums[1] == 7)
end
print check_array([1, 4]),"\n"
print check_array([7, 5]),"\n"
print check_array([5, 2])
Output:
true true false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to concatenate array of arrays into one.
Next: Write a Ruby program to check whether a given array of integers of length 2 does not contain a 6 or a 9.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework