Design comparison
Solution retrospective
Although this was pretty simple, I quite enjoyed writing the javascript for this.
I noticed now afterwards that it does not highlight the correct day as it should. At the moment its highlighting monday instead of sunday, any idea why this might be happening?
Community feedback
- @adamwozherePosted over 2 years ago
Hi Robin,
I wonder if in the design the blue highlight colour is meant to signify the highest value for that week, but I like the idea of highlighting the current day.
To answer your question; looking on MDN for
Date.getDay()
- you might have missed it; but the zero index starts with Sunday not Monday!So incrementing
i
by one then using modulo%
to keep the value in 0 - 6 range should work:if( (i + 1) % 7 == date.getDay() ) { bars[i].classList.add("today"); }
Hope that helps!
Marked as helpful1@MountainbeachPosted over 2 years ago@adamwozhere I didn't even think about that, I just assumed it was for marking the current day since the highest value is pretty clear regardless. But either works I suppose!
Dangit that seems like a weird quirk to me, but I suppose what is seen as the first day of the week is different all over the world. Does the modulo actually make a difference in this case? Since
i
should only go to 6 anyways cause of thebars.length
.Thank you for the help!
0@adamwozherePosted over 2 years ago@Mountainbeach No you are right, the challenge does say that the current day should be highlighted a different colour -- I didn't read that bit!
Yes you do need the modulo -- as you get to your 6th index in the loop which is your Sunday bar, you need that to equal 0 because of
date.getDay()
but incrementing it by 1 equals 7:let i = 6; if ( (i + 1) == date.getDay() ) { ... // if ( (7) == 0 ) { ... // returns false
With modulo:
7 % 7
equals 0, as 7 divided by 7 has a remainder of 0.Hope that makes sense!
Marked as helpful1
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