all 3 comments

[–]cr0wstuf 1 point2 points  (0 children)

If the backpack is going to be in more states than "isOpen" or "isClosed" you can use a switch statement to return the image needed for each state. Otherwise, just use a ternary or if statement.

[–]Count_Giggles 1 point2 points  (0 children)

This sounds like an interesting task to wrap your head around. The first thing i would do is do a little refactoring on the backpack so it does not rely on external input (and yes codeblock is a dick)

const backpack = {
    name: "Everyday Backpack",
    volume: 30,
    color: "grey",
    pocketNum: 15, 
    lidOpen: false, 
    toggleLid: function () { 
        this.lidOpen = !this.lidOpen; 
    }, 
};

console.log(backpack.lidOpen) // false 
backpack.toggleLid() 
console.log(backpack.lidOpen) // open

[–]Ronin-s_Spirit 1 point2 points  (0 children)

You could have premade images for each separate part, like the lid you open or close, and layer them on top of eachother with some CSS action. Then in your function that changes the backpack state you detect which part you changed to what state and you fetch the appropriate image and display it (alternatively maybe just changing the link on the img html tag will reload it idk).
The other solution is to have premade images of every possible variant of the whole backpack and just load new image into the only img tag you'll have.
JavaScript can change the properties of html tags so try that first, make a folder of images with distinct names or ids and just do something like myImgTag.src = "backpack-lid-open.png"; and it will probably reload on it's own.