Ruby Array Exercises: Check whether a given value appears everywhere in a given array
Ruby Array: Exercise-37 with Solution
Write a Ruby program to check whether a given value appears everywhere in an given array. A value is "everywhere" in an array if it presents for every pair of adjacent elements in the array.
Ruby Code:
def check_array(nums)
i = 0;
val = 3
while i < nums.length
if(nums[i] != val && nums[i+1] != val)
return false
end
i = i + 1
end
return true
end
print check_array([3, 7, 3, 3]),"\n"
print check_array([2, 8, 2, 9]),"\n"
print check_array([3, 8, 5, 4, 3, 7]),"\n"
Output:
true false false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check whether it contains no 3 or it contains no 5.
Next: Write a Ruby program to check whether an given array contains a 3 next to a 3 or a 5 next to a 5, but not both.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/ruby-exercises/array/ruby-array-exercise-37.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics