# Help with error on function

**URL:** https://forum.makecode.com/t/help-with-error-on-function/28428
**Category:** micro:bit
**Created:** [April 25, 2024, 5:25pm UTC](https://forum.makecode.com/t/help-with-error-on-function/28428 "2024-04-25T17:25:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Student1](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Student1](https://forum.makecode.com/u/Student1)
#### Post date: [April 25, 2024, 5:25pm UTC](https://forum.makecode.com/t/help-with-error-on-function/28428/1 "2024-04-25T17:25:30Z")

</div>

```auto
function setAnswer () {
    list = []
    list = [images.createImage(`
        . . . . .
        . . . . #
        . . . # .
        # . # . .
        . # . . .
        `), images.createImage(`
        # . . . #
        . # . # .
        . . # . .
        . # . # .
        # . . . #
        `), images.createImage(`
        . . . . .
        . # . # .
        . . . . .
        # # # # #
        . . . . .
        `)]
}
input.onGesture(Gesture.Shake, function () {
    setAnswer()
    rollAnswer()
})
function rollAnswer () {
    list.removeAt(randint(0, 3)).showImage(0)
}
let list: Image[] = []
basic.showLeds(`
    . # # # .
    . # . # .
    . # # # .
    . # . # .
    . # # # .
    `)
basic.clearScreen()

```

I keep getting error code for “Deferencing null/undefined value” I have been trying to fix it myself for quite some time and would like to know if someone has any ideas!

---

<div class="post-metadata">

### Author: ![dsssssssss9](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/dsssssssss9/32/861_2.png) [@dsssssssss9](https://forum.makecode.com/u/dsssssssss9)
#### Post date: [April 25, 2024, 5:50pm UTC](https://forum.makecode.com/t/help-with-error-on-function/28428/2 "2024-04-25T17:50:40Z")

</div>

HI

try this version of your code - it works ok for me

All i have done is change the randint value in the rollanswer function to pick a number from 0 to 2.

remember that most languages count from 0 ( zero )

so if you have 3 items the 1st is item 0 , 2nd is item 1 ,3rd is item 2

```auto
function setAnswer () {
    list = []
    list = [images.createImage(`
        . . . . .
        . . . . #
        . . . # .
        # . # . .
        . # . . .
        `), images.createImage(`
        # . . . #
        . # . # .
        . . # . .
        . # . # .
        # . . . #
        `), images.createImage(`
        . . . . .
        . # . # .
        . . . . .
        # # # # #
        . . . . .
        `)]
}
input.onGesture(Gesture.Shake, function () {
    setAnswer()
    rollAnswer()
})
function rollAnswer () {
    list.removeAt(randint(0, 2)).showImage(0)
}
let list: Image[] = []
basic.showLeds(`
    . # # # .
    . # . # .
    . # # # .
    . # . # .
    . # # # .
    `)
basic.clearScreen()

```

---

<div class="post-metadata">

### Author: ![bsiever](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/bsiever/32/1032_2.png) [@bsiever](https://forum.makecode.com/u/bsiever)
#### Post date: [April 26, 2024, 3:41pm UTC](https://forum.makecode.com/t/help-with-error-on-function/28428/3 "2024-04-26T15:41:26Z")

</div>

Hi @Student1 — welcome to the forum.

@dsssssssss9 had a great point about the range. `randInt` uses inclusive values for min and max.

You may also want to simply use `get` or the `[]` notation rather than `removeAt`. Then you don’t need to repeatedly do `setAnswer()` to re-build the list. For example, here’s a slight variation with fewer images:

```auto
let list = [images.createImage(`
        . . . . .
        . . . . #
        . . . # .
        # . # . .
        . # . . .
        `), images.createImage(`
        # . . . #
        . # . # .
        . . # . .
        . # . # .
        # . . . #
        `)]
input.onGesture(Gesture.Shake, function () { 
    list[randint(0,1)].showImage(0)
})

```

If you want to make it a little more flexible, you could base the argument to `randInt` on `list.length`. That way it would work with whatever is in the list (as long as the list isn’t empty) and you wouldn’t have to modify the part that does the `randInt` if you change the number of things you want in the list.

---

<div class="post-metadata">

### Author: ![Student1](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Student1](https://forum.makecode.com/u/Student1)
#### Post date: [May 2, 2024, 7:21pm UTC](https://forum.makecode.com/t/help-with-error-on-function/28428/4 "2024-05-02T19:21:01Z")

</div>

Thank you so much! I genuinely didn’t realize I had a mis-input on the variable.
