Design comparison
Solution retrospective
JS script not great, but it works ) I will be glad for a hint how to write it better if possible. Thanks
Community feedback
- @Ahmed96MahPosted over 2 years ago
Hello Alexei,
These steps could generally improve your JavaScript code:
1- You could use arrow functions instead of regular ones. For Example you could say:
const toDark = () => { toggle.classList.remove('white-theme-toggle') .... }
2 - You could use const instead of let for the first 5 lines, as it is always best practice to use it when possible
3- You could use a callback function instead of a function expression for the event listener. for example:
const toggleMode = () => { if (toggle_ball.classList.contains('toggle__ball_dark')) { .... }else { .... } }; toggle.addEventListener("click", toggleMode);
4- You could use foreach loop or 'for of' loop instead of regular for loops .
card.foreach((item) => { item.classList.toggle('white-theme-card-bg'); item.classList.toggle('dark-theme-card-bg'); });
And generally, I think that you could merge both "toDark" & "toWhite" functions into one function and use 'toggle' instead of "add & remove" since the website will always have an initial style and all what you want is to flip or "toggle" the classes.
Have a nice day ;)
Marked as helpful1@Batareika007Posted over 2 years ago@Ahmed96Mah
Wow Thank for that! It's very useful information.
I will check all of it.
Have a great day !
0@Ahmed96MahPosted over 2 years ago@Batareika007
Thank You ;)
By the way, nice job on making the page take the initial theme (dark-mode or light-mode) of the browser. I really liked that idea :)
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord