Ruby Array Exercises: Check whether 7 appears as either the first or last element in a given array
Ruby Array: Exercise-2 with Solution
Write a Ruby program to check whether 7 appears as either the first or last element in a given array. The array length must be 1 or more.
def check_array(nums) return (nums == 7 || nums[nums.length-1] == 7) end print check_array([1, 2, 7]),"\n" print check_array([7, 1, 2, 3]),"\n" print check_array([14, 7, 1, 2, 3])
true true false
Ruby Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- 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