Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 3 years ago

Responsive Rating Card Component

Joshua Watson•150
@JoshLanderz
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Hi, I'm having problems on the JS side of things :(

I tried throwing if else statements inside the submit eventListener but it didn't quite turn out the way I wanted it too. Not sure if this was the way to go either (clearly not lol) as I'm not that great at JavaScript. If anyone has a solution to this, it would be much appreciated.

PS: I'll send a virtual bro/sis hug if u want as well haha....

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Elaine•11,360
    @elaineleung
    Posted about 3 years ago

    Hey Joshua, I just had a look at your code, and I do agree, the if/else statements are somewhat verbose. The bigger problem, however, is that it doesn't seem like the rating is working the way it should, most likely because buttons don't have a click or clicked attribute. From my end at least, any number I choose would end up with a 5 rating.

    Anyway, I'm going to make a few suggestions that you can try out:

    (1) Group your five rating-el buttons together:

    const ratingEls = document.querySelectorAll('.rating-el');
    

    The good thing is you're using buttons and not plain divs, which means there's info such as value that you can take from the buttons. Once you group them together, you can iterate/loop through them and not need to write out five separate variables for each one.

    (2) Add an event listener for the buttons to observe which button is clicked so that you can track the value. To do that, I'll create a variable called selected set to null and also use a forEach() to add an event listener to the ratingEls button group created earlier; the forEach() would just loop through the group so there's no need to write out five different event listeners. The event listener would contain a function that says, if this button is clicked, set selected to the value of the button.

    let selected = null
    ratingEls.forEach((btn) => {
      btn.addEventListener('click', () => selected = btn.value);
    });
    

    (3) Replace the if/else statements: Using the information from the selected variable, we can then rewrite the valueSelection() function like this with the help of template literals (as in, ${}):

    function valueSelection() {
      selectionEl.textContent = `You selected ${selected} out of 5`;
    }
    

    The whole thing would look something like this:

    const ratingEls = document.querySelectorAll('.rating-el');
    let selected = null;
    
    ratingEls.forEach((btn) => {
      btn.addEventListener('click', () => selected = btn.value);
    });
    
    submitEl.addEventListener('click', function () {
      if (selected) {   // this prevents the screen from changing if nothing is selected
        ratingWrapper.style.display = 'none';
        ratingWrapper2.style.display = 'grid';
        valueSelection();
      }
    });
    
    function valueSelection() {
      selectionEl.textContent = `You selected ${selected} out of 5`;
    }
    

    I know this might be beyond your knowledge right now, but I think you can at least use some of this as reference and then do some more research on your own. Anyway, hope this is of use to you!

Join 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

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub