What the code is supposed to do
The function printPositiveAndNegative receives an Array[Int] named nums and prints one line for each element. A number below zero is replaced by the word negative. Any other number, zero included, is printed as it is. For [2, -12, 0, 4, -5, 3] the expected output is:
2
negative
0
4
negative
3
The code you start with produces a different output: an extra positive line before each positive number, and the negative numbers printed as they are.
def printPositiveAndNegative(nums: Array[Int]): Unit = {
for (num <- nums) {
if (num > 0) {
println("positive")
}
println(num)
}
}
// Do not change this code:
def mainFunction(): Unit = {
printPositiveAndNegative(Array(2, -12, 0, 4, -5, 3))
}
What goes wrong
There are three separate problems in the five lines of the loop body.
- The condition picks the wrong numbers.
num > 0is true for positive numbers, but the special message belongs to numbers less than zero. This is why2,4and3get an extra line while-12and-5get nothing. - The message is the wrong word. The task asks for
negative, and the code printspositive. - The last
printlnruns every time. It sits after theif, not inside a branch, so every number is printed even when the message was already printed for it. A number that gets the message should not also be printed.
So for 2 the code prints positive and then 2. For -12 it skips the message and prints -12. For 0 it prints just 0, which happens to be right, because zero is not greater than zero and not less than zero either.
Fixing it step by step
- Change the test to
num < 0. Now the branch is entered only for numbers below zero. Zero stays out of it, as the task requires. - Print
"negative"in that branch. Replace the text of the existingprintln. - Put the number printing in an
elsebranch. Theelseruns exactly when the condition is false, which is when the number is zero or positive. Now every element produces exactly one line.
The fixed code in Scala
def printPositiveAndNegative(nums: Array[Int]): Unit = {
for (num <- nums) {
if (num < 0) {
println("negative")
} else {
println(num)
}
}
}
def mainFunction(): Unit = {
printPositiveAndNegative(Array(2, -12, 0, 4, -5, 3))
}
The output for the example array is:
2
negative
0
4
negative
3
Some details of this Scala code:
- Looping over an array.
for (num <- nums)visits the elements in order, binding each one tonum. No index is needed when the position doesn't matter. Array(...)builds an array.Array(2, -12, 0, 4, -5, 3)creates anArray[Int], with the element type inferred from the numbers.printlnaccepts both types. The two branches print aStringand anInt, and both work with the same function.- Negative numbers print with a minus sign. The output of
println(-12)would be-12, which is why the original code showed that value on its own line.
Common mistakes
- Using
num <= 0. That would printnegativefor zero. The task wants the number itself when it is greater than or equal to zero. - Keeping the final
println(num)outside the branches. Negative numbers would then print twice, asnegativefollowed by the number. - Checking
num > 0in theifand swapping the branches. This fails for zero: it would go to the branch for non-positive numbers and printnegativefor it. - Changing the call in
mainFunction. The lines below the comment stay as they are.
Checking your fix
Run the code in the editor above and compare the six lines of output with the list at the top. The 0 in the middle of the array is the value worth checking: it must appear as a plain 0.
