Help with error on function

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!

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

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()

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:

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.

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