Hi, beginner here. `//wins - horizontal` `fun hor...
# announcements
i
Hi, beginner here.
//wins - horizontal
fun horizontalWin(letter: String, listIn: listOf<String>() ): boolean {
var counter = 0
for (element in listIn ) {
if (element == letter) {
counter++
}
}
if (counter == 3) return true
}
fun main(args: Array<String>) {
var row1 = listOf("X","O","_")
var checkForWin = horizontalWin("X", row1)
}
It s a bit rough and I am not sure if it makes sense, but I want to pass a list (or array) in and test it. Thank you.
s
next time please use triple backticks ` ``` ` to mark the beginning and end of a code block
Copy code
fun main() {
  println("hello world")
}
r
also, is there a question? 🤔
i
Ah sorry, totally missed that from my draft! apologies for the formatting too, anyway, I want to pass a list as a parameter but, I am getting 'unresolved reference' for the list parameter in the function, is it possible to pass a list/array as a parameter to a function? If so, how? thank you.
s
ohhh, I see, just reread your message
listOf()
is a helper function to approximate list literals
the type is
List<T>
(also
MutableList<T>
if you need write methods)
Copy code
fun horizontalWin(letter: String, listIn: List<String>): Boolean {
  ...
}
@ImranH (in case you don’t have thread notifications enabled)
i
Ah, thank you, I will give that a go. Apologies for the confusion again.
Worked like a charm 👍
👍 1
a
@ImranH also, consider the following equivalent but way more consise approach
Copy code
fun horizontalWin(letter: String, listIn: List<String>): Boolean {
  val matches = listIn.filter { it == letter } 
  return matches.size >= 3
}
, or taken a step further
Copy code
fun horizontalWin(letter: String, listIn: List<String>) = listIn.filter { it == letter }.size >= 3
s
I mean, I figure that’d be tangential to the goal of learning Kotlin basics
you really could just do
listIn.count { it == letter } >= 3
👌 1
i
Thanks guys, I will have a play with those too. Appreciate it.