0
0
Rubyprogramming~10 mins

Unless for negated conditions 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 a message unless the number is zero.

Ruby
number = 5
puts "Number is not zero" [1] number == 0
Drag options to blanks, or click blank then click option'
Auntil
Bunless
Cwhile
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' will print the message only when number is zero.
Using 'while' or 'until' is for loops, not conditions.
2fill in blank
medium

Complete the code to skip printing when the word is empty using unless.

Ruby
word = "hello"
puts word [1] word.empty?
Drag options to blanks, or click blank then click option'
Aif
Bwhile
Cuntil
Dunless
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' will print only when the word is empty, which is the opposite of what we want.
Using 'while' or 'until' is for loops, not conditions.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword to print unless the number is negative.

Ruby
number = 3
puts "Positive or zero" [1] number < 0
Drag options to blanks, or click blank then click option'
Aunless
Bif
Cwhile
Duntil
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' will print only when the number is negative, which is wrong here.
Using 'while' or 'until' is for loops, not conditions.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values, but only for words longer than 3 letters using unless.

Ruby
words = ["cat", "elephant", "dog", "lion"]
lengths = {}
words.each { |word| lengths[word] = word.length [1] word.length [2] 3 }
Drag options to blanks, or click blank then click option'
Aunless
B>
C<=
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' changes the logic.
Using '>' instead of '<=' in the condition will include wrong words.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, but only for words not empty using unless.

Ruby
words = ["", "apple", "", "banana"]
result = {}
words.each { |word| result[word.[1]] = word.[2] [3] word.empty? }
Drag options to blanks, or click blank then click option'
Aupcase
Blength
Cunless
Dif
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'if' instead of 'unless' changes the logic.
Forgetting to convert words to uppercase.