w3resource

Ruby Array Exercises: Check whether it contains no 3 or it contains no 5

Ruby Array: Exercise-36 with Solution

Write a Ruby program to check whether it contains no 3 or it contains no 5.

Ruby Array Exercises: Check whether it contains no 3 or it contains no 5

Ruby Code:

def check_array(nums)
   noThree = true, noFive = true;
   i = 0;
   while i < nums.length && (noThree || noFive)
        if(nums[i] == 3)
			noThree = false
		elsif(nums[i] == 5)
			noFive = false
		end	
	i = i + 1
   end
   return (noThree || noFive)
end
print check_array([3, 7, 3, 3]),"\n"
print check_array([2, 8, 2, 9]),"\n"
print check_array([3, 8, 5, 9]),"\n"

Output:

true
[true, true]
false

Flowchart:

Flowchart: Check whether it contains no 3 or it contains no 5

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check whether every element is a 3 or a 5 in a given array of integers.
Next: 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.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://www.w3resource.com/ruby-exercises/array/ruby-array-exercise-36.php