Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Kotlin for Java Developers Modelling Solitaire Finishing the Pieces

Shouldn't the answer be 012? Aren't 0 and 2 inclusive in the range?

Also the answer in the question 3 contradicts with the answer in the first question

2 Answers

andren
andren
28,520 Points
for (var i in 0..2) { 
    println(i) 
}

Will not produce "0 1 2" because it will actually produce an error. This error to be precise:

'var' on loop parameter is not allowed

I have not completed the "Kotlin for Java Developers" course yet, so I don't know if it is brought up in the course. But neither var nor val is supported when declaring an index variable for a for loop.

The third quiz code:

for (i in 23..25) { 
    println(i) 
}

Correctly omits the var keyword and therefore will produce "23 24 25" since it is an inclusive range as you correctly noted.

Thank you for confirming that up. I too noticed that difference but was not sure if that's not allowed in the for loop.