#Golang: Append to slice in card-tricks not working

1 messages · Page 1 of 1 (latest)

meager quail
#

Hi, I'm trying to solve card-tricks in the Golang track, setItem specifically:

func SetItem(slice []int, index, value int) []int {
    if (index > len(slice) - 1) || (index < 0) {
        append(slice,value)
    } else {
        slice[index] = value
    }
    return slice
}

I'm not sure what I'm doing wrong, I append the value to the slice if the index is out of bounds.

Thank you

mental aspen
#

What does the append() function do?

tropic grove
#

Heyy am stuck on the first lesson of the python course

mental aspen
tropic grove
#

ok thank u

mental aspen
#

@meager quail ^^

meager quail
#

the append function appends variable nr of elements to a slice

mental aspen
#

It returns a new slice with the elements added. It doesn't modify the slice in place.

#

The pattern is slice = append(slice, values...)

meager quail
#

ah my bad, thank you so much

mental aspen
#

You're very welcome. Did you get it to pass the tests?

meager quail
#

yes it passes now

mental aspen
#

🎉