Skip to content

Instantly share code, notes, and snippets.

@SpaYco
Last active March 8, 2022 06:36
Show Gist options
  • Save SpaYco/72aa67281a40fe5f2019d86da394c63e to your computer and use it in GitHub Desktop.
Save SpaYco/72aa67281a40fe5f2019d86da394c63e to your computer and use it in GitHub Desktop.
######## Make any even/odd calculator with this method ########
######################## Get Crazy ###########################
def getCrazy(n)
result = "case x\n"
1..n.times do |number|
result += " when #{number}\n"
result += " p '#{number % 2 == 0 ? 'even': 'odd'}'\n"
end
result += "else\n p 'I\\\'m not there yet'\nend"
end
# getCrazy(500)
@MohamedBechirMejri
Copy link

MohamedBechirMejri commented Mar 8, 2022

You can use .even? method instead of % 2 == 0.

########################  Get Crazy ###########################   
def getCrazy(n)
	result = "case x\n"
		1..n.times do |number|
		result += "  when #{number}\n"
		result += "    p '#{number.even? ? 'even': 'odd'}'\n"
	end
	result += "else\n    p 'I\\\'m not there yet'\nend"
	puts result
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment