P5.js code for creating a calculator tip app confused

yes basically img is not defined at first, so either

  • initialize img in setup by assiging one of the images
  • or skip image() if img is not defined

The latter is what Chrisir suggested, but strictly speaking, when a variable is not defined yet, it is undefined so you should test like img !== undefined