Design comparison
Solution retrospective
Any suggestions on how to refactor and clean up my discounting functions would be super helpful! They feel a little rough, they're quite nested and do feel repetitive.
Feel free to comment on the PR if that's helpful or easier!
Community feedback
- @visualdennissPosted over 1 year ago
Wow your solution looks great! Responds well to all browser sizes as well. Congrats on completing the challenge successfully.
Regarding, "clean up my discounting functions would be super helpful", if thats the function you refer:
discountToggle.addEventListener("input", function() { if(discountToggle.checked) { insertNewPrice(inputSlider.value); inputSlider.addEventListener("input", function(e) { return ( createNewPrice(e.target.value) ); }); } if(!discountToggle.checked) { spanPriceDisplay.style.textDecoration = "none"; discountedPriceDisplay.innerText = ""; inputSlider.addEventListener("input", function() { return ( discountedPriceDisplay.innerText = "" ); }); } });
you can simply take the repetitive part inside a function and pass the unique parts as props to that and you can call the function for each if case instead.
e.g.:
inputSlider.addEventListener("input", function() { return ( discountedPriceDisplay.innerText = "" );
this part is called twice, but it just returns different things. So what it should return could be defined in the args of the function.
You could reduce the eventListener to this by extracting those duplications that way.
discountToggle.addEventListener("input", function() { if(discountToggle.checked) { callbackFunction(arg1, arg2, callback2, returnValue1) } else {callbackFunction(diffArg, diffArg2, callback1, returnValue2) }}
Hope this was helpful!
Marked as helpful1@marissahuysentruytPosted over 1 year ago@visualdenniss Thanks so much for your feedback! I will try to make those
if
blocks functions first, then pass them into thatdiscountToggle
event listener. That's a great idea! I had a feeling I was making that event listener do too much π1@visualdennissPosted over 1 year ago@marissahuysentruyt Happy to hear it was helpful! Keep it up the great work and good luck on the upcoming challenges!
0
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