I struggle with refactoring my code for a color switch. Color ends up being undefined.
How can I pass the color variable from the main module to the modules while passing the callback at the same time?
main.js
import { changeColor } from "./changeColor.js"
import { showColor } from "./showColor.js"
let color = "green"
document.getElementById("button").addEventListener("click",() => { changeColor(showColor) })
changeColor.js
function changeColor(callback) {
if (color === "green") {
color = "red"
}
else {
color = "green"
}
callback()
}
export { changeColor };
showColor.js
function showColor() {
console.log(color);
}
export { showColor };