I am not sure about the previous code is given, but that should not be the way you force the accordion to be closed.
The correct way is that you use the new Accordion element instead of the Classic accordion. In each accordion option there you can find an option to select whether you want to have the accordion item opened or closed:
Now if you want to have the accordions opened in the desktop and closed in the mobile, You can add the Accordion element and set proper options for the desktop. Then duplicate the same element and this time set it to close. Then use the hide based on the breakpoint options to hide one in desktop and show in mobile and vice versa.
The method above will be bulletproof and will not break in the future updates in contrary to the custom codes that might break.