#Store value before it becomes undefined

1 messages · Page 1 of 1 (latest)

bronze perch
#

I have one variable named hit containing one array, and I have the next codeJavascript if (hit.length > 0) { const entity = getHit(hit); if (entity !== undefined) { entity.triggerEvent(eventName_1); } }which triggers one event (asigned to the variable named eventName_1) when the getHit method (which is a "for loop" to extract the values of the array) returns a value different from undefined from the array named hit.

And I need to trigger (triggerEvent) another different event (eventName_2) in the same entity when the array hit is empty (hit.length === 0), but triggerEvent fires an error when a variable containing undefined calls it, so I think I need to define a new array (e.g. a variable named hitList) containing the values of the array named hit and make it so it never becomes empty, that way I can call the getHit method with the new array (getHit(hitList)) just when getHit(hit) becomes undefined.

#

I tried this way:```Javascript
let hitList; // NEW

    if (hit.length > 0) {
        hitList = hit;     // NEW. Assign the value of 'hit' to 'hitList'
        const entity = getHit(hit);
        if (entity !== undefined) {
            entity.triggerEvent(eventName_1);
        }
    } else if (hit.length === 0) {         // NEW
        const entity = getHit(hitList);    // Line number 17
        entity.triggerEvent(eventName_2);
    }```But the line number 17 fires this error:```[Scripting][error]-TypeError: cannot read property 'triggerEvent' of undefined    at <anonymous>```so I don't know how to run triggerEvent on the stored entities (array `hitList`), what am I doing wrong?
bronze perch
#

Store value before it becomes undefined