diff --git a/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts new file mode 100644 index 0000000..af4d0a7 --- /dev/null +++ b/src/syntax/functionParameterConfusion/FunctionParameterConfusion.kts @@ -0,0 +1,11 @@ +package syntax.functionParameterConfusion + +val items = listOf("a", "b", "c") + +println(items.joinToString { " + " }) + +// What will the output be? +// a) "a + b + c" +// b) "a b c" +// c) "a, b, c" +// d) None of the above diff --git a/src/syntax/functionParameterConfusion/Rationale.md b/src/syntax/functionParameterConfusion/Rationale.md new file mode 100644 index 0000000..3a3cf7f --- /dev/null +++ b/src/syntax/functionParameterConfusion/Rationale.md @@ -0,0 +1,9 @@ +Correct answer: **d) None of the above** - the created string is actually `" + , + , + "` + +`joinToString` has a default argument for every parameter, including the _separator_. The code uses curly brackets +(`{ ... }`) which therefore represents the _transform_ function, transforming every element. The parameter (`it`) of +the transform function was omitted. + +The lesson from this: Pay attention to the difference between parentheses and curly brackets and verify that you are +passing a value for the correct parameter. Also be careful when using suggestions from the IDE, here IntelliJ IDEA +would have suggested the variant with _transform_ function first. diff --git a/src/syntax/gameOfLife/Rationale.md b/src/syntax/gameOfLife/Rationale.md index 44fcad4..4c53eb6 100644 --- a/src/syntax/gameOfLife/Rationale.md +++ b/src/syntax/gameOfLife/Rationale.md @@ -1,6 +1,6 @@ Correct answer: **b) 3** -* Languges without semicolons need to make decisions when a statement ends +* Languages without semicolons need to make decisions when a statement ends * Lines starting with '+' can be interpreted as starting with unary '+' * Groovy has the same problem, but JavaScript hasn't * Fix by keeping '+' on the previous line