Skip to content

Kotlin Q1's answer is incorrect #7096

Open
@abdallahalaraby

Description

@abdallahalaraby

Q1. You would like to print each score on its own line with its cardinal position. Without using var or val, which method allows iteration with both the value and its position?

fun main() {
  val highScores = listOf(4000, 2000, 10200, 12000, 9030)
}

Actual Answer

  • .withIndex()
  • .forEachIndexed()
  • .forEach()
  • .forIndexes()

Expected Answer

  • .withIndex()
  • .forEachIndexed()
  • .forEach()
  • .forIndexes()

Explanation:
withIndex() returns an Sequence<IndexedValue<T>> which still holds a Sequence but does not iterate through it. The correct answer should be .forEachIndexed() which does allow iteration through the Sequence with both the value and its position.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions