0
0
Rubyprogramming~10 mins

If, elsif, else statements in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print "Positive" if the number is greater than zero.

Ruby
number = 5
if number [1] 0
  puts "Positive"
end
Drag options to blanks, or click blank then click option'
A<
B<=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes the condition to check for numbers less than zero.
Using '==' checks for equality, not greater than.
2fill in blank
medium

Complete the code to print "Zero" if the number equals zero.

Ruby
number = 0
if number > 0
  puts "Positive"
elsif number [1] 0
  puts "Zero"
end
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' or '<' instead of '==' will not correctly check for zero.
Using '!=' checks for not equal, which is incorrect here.
3fill in blank
hard

Fix the error in the code to print "Negative" if the number is less than zero.

Ruby
number = -3
if number > 0
  puts "Positive"
elsif number == 0
  puts "Zero"
else
  puts "[1]"
end
Drag options to blanks, or click blank then click option'
ANegtive
Bnegative
CNegative
DNeg
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'negative' when the output expects capitalized.
Misspelling the word as 'Negtive'.
4fill in blank
hard

Fill both blanks to complete the code that prints "Even" if the number is even, otherwise "Odd".

Ruby
number = 4
if number [1] 2 == 0
  puts "Even"
else
  puts "[2]"
end
Drag options to blanks, or click blank then click option'
A%
BOdd
Codd
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '%' for checking evenness.
Printing 'odd' with lowercase instead of 'Odd'.
5fill in blank
hard

Fill all three blanks to complete the code that categorizes a number as positive, zero, or negative.

Ruby
number = -1
if number [1] 0
  puts "Positive"
elsif number [2] 0
  puts "Zero"
else
  puts "[3]"
end
Drag options to blanks, or click blank then click option'
A>
B==
CNegative
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping '<' and '>' operators.
Using lowercase 'negative' instead of 'Negative'.
Using '!=' instead of '==' for zero check.