Start learning free

LESSON 131 OF 414 · LOOPS & CONDITIONALS

Print Even Numbers in Scala: Fixing the Buggy Code

Practice Read the full explanation

Inside the code editor we've tried to write a function that takes an array nums as argument and prints to the console the even numbers from that array.

So when we called printEvenNumbers(Array(2, 1, 0, 4, 3)), we expected our code to print:

2
0
4

but it seems like we made some mistakes because when we run our code, it prints:

even
even

Assignment:

Your task is to fix our function such that it prints the even numbers from the array.

STEP 1 OF 1
    Ctrl + Enter to check

    SCALA · LOOPS & CONDITIONALS

    Buggy Code Print Even Numbers in Scala, explained

    What the code is supposed to do

    The method printEvenNumbers takes an array nums and should print its even numbers, one per line. For Array(2, 1, 0, 4, 3) the expected output is 2, 0 and 4. The code you start with prints something else:

    even
    even
    

    Here is the code:

    class Solution {
        def printEvenNumbers(nums: Array[Int]): Unit = {
            for (num <- nums) {
                if (num % 2 == 1) {
                    println("even")
                }
            }
        }
    
        // Do not change this code:
        def mainFunction(): Unit = {
            printEvenNumbers(Array(2, 1, 0, 4, 3))
        }
    }
    

    What goes wrong

    There are two separate mistakes, both inside the loop. Each one shows up in the output.

    Mistake 1: the condition selects odd numbers

    The operator % gives the remainder after division. An even number divides by 2 with remainder 0, and an odd number leaves remainder 1. So num % 2 == 1 is true for the odd numbers in the array, which are 1 and 3, and false for the even ones. That is why exactly two lines are printed: the array has two odd numbers.

    The condition should select numbers whose remainder is 0.

    Mistake 2: the text is printed instead of the number

    println("even") prints the characters inside the quotes, and it does so every time it runs. A string in quotes is a fixed piece of text. It has nothing to do with the variable num. To print the value of a variable, pass the variable itself, without quotes: println(num).

    Fixing it step by step

    1. Change the comparison. Replace num % 2 == 1 with num % 2 == 0. Now the if body runs for 2, 0 and 4.
    2. Change what is printed. Replace println("even") with println(num), so each even number is shown.
    3. Leave the rest alone. The loop for (num <- nums) already visits every element, and mainFunction is marked as not to be changed.

    How to find bugs like these

    Compare the actual output with the expected output. Two lines of even instead of three numbers tells you two things: the printed text is wrong, and the number of lines is wrong. The count points at the condition, and the content points at the print statement. Walking through the array by hand with the original condition confirms it: 2 gives remainder 0 and is skipped, 1 gives remainder 1 and prints, and so on.

    The fixed code in Scala

    class Solution {
        def printEvenNumbers(nums: Array[Int]): Unit = {
            for (num <- nums) {
                if (num % 2 == 0) {
                    println(num)
                }
            }
        }
    
    
        def mainFunction(): Unit = {
            printEvenNumbers(Array(2, 1, 0, 4, 3))
        }
    }
    

    For the sample array, this prints:

    2
    0
    4
    

    Some Scala details are worth knowing:

    • Zero is even. 0 % 2 is 0, so 0 is printed, as the expected output requires.
    • Negative numbers and %. The result of % takes the sign of the left operand, so -3 % 2 is -1, not 1. A test for odd numbers written as num % 2 == 1 therefore misses negative odd numbers. Comparing with 0 avoids the problem: -4 % 2 == 0 is true, and -3 % 2 == 0 is false.
    • The loop variable. num is a value that the loop creates on each pass. It holds the current element and isn't an index.

    Edge cases and common mistakes

    • Arrays with no even numbers. Array(1, 3) prints nothing, and that is correct.
    • Empty array. Nothing is printed.
    • Fixing only one mistake. Changing just the condition still prints the word even three times. Changing just the print line prints 1 and 3 instead.
    • Quotes around the variable. println("num") prints the word num, not the value.
    • Printing instead of returning. The method's return type is Unit, so it prints its results and returns nothing.

    Testing

    • Array(2, 1, 0, 4, 3) prints 2, 0 and 4.
    • Array(-4, -3, 7) prints -4.
    • Array(5, 9) prints nothing.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free