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

Interactive Rating Component using SASS, CSS Flexbox and JavaScript

accessibility, sass/scss, vanilla-extract
Yaika Race•290
@YaikaRace
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


This is my first challenge with JavaScript, I have never used JavaScript before in my life, I hope I did well and I hope you like it.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

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

    Hi Yaika Race, great job in completing this first JS challenge! Everything works well, and I like how you used some logic to handle what happens when nothing is selected. Great work also in displaying a warning reminder!

    Two suggestions I have here:

    1. Instead of using <p>, use an interactive element like <button> instead, as p is really not meant for this task (it's more of a pure text element), and buttons are more suited since they handle actions. Buttons also allow for attributes such as value, and so you can store a value that you can later retrieve in the JS (e.g., value="1").

    2. Great job using the forEach! I see that you also have the idx in your function and that you later use the index for displaying the score. A simpler way that does not involve adding 1 to the index would be to just retrieve the textContent, or if you do use a value attribute in the button as I mentioned above, then you can use event.target.value or even just the element's value. To see this in action, you can check out this mini CodePen I made for this challenge: https://codepen.io/elaineleung/pen/RwMqMxZ

    Anyway, really great job on the whole in writing out all the JS here! 😊

    Marked as helpful
  • P
    John Mirage•1,590
    @john-mirage
    Posted almost 3 years ago

    Well done!

    A personnal tip that you may find interesting (or not):

    Selectors

    • I always use different selectors for CSS and JS.
    • I always use classes for CSS to have the same specificity. MDN - specificity

    Classes for CSS

    this example use the BEM methodology (recommended).

    HTML

    <ul class="list">
      <li class="list__item">
        <a class="list__link list__link--blue">home</a>
      </li>
      <li class="list__item">
        <a class="list__link list__link--red">about</a>
      </li>
    </ul>
    

    SCSS

    .list {
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: row;
      align-items: center;
    
      &__item {
        padding-left: 1rem;
        padding-right: 1rem;    
      }
    
      &__link {
        text-decoration: none;
        color: green;
    
        &--blue {
          color: blue;
        }
    
        &--red {
          color: red;
        }
      }
    }
    

    data attributes for JS

    Using id is great when you only have one element. But when you need to select multiple elements, you need to use a class wich is only used for CSS. Data attributes can be used instead of classes and ids to select elements with JS. more info here

    Make sure that the data-id used for only one element is unique on the page (like an id);

    HTML

    <main class="app" data-id="app"></main>
    <ul class="list">
      <li class="list__item" data-id="list-item"></li>
      <li class="list__item" data-id="list-item"></li>
    </ul>
    

    JS

    const appElement = document.querySelector('[data-id="app"]');
    const listItemElements = document.querySelectorAll('[data-id="list-item"]');
    

    Why it may be better:

    • By using only classes for CSS, we avoid specificity issues.
    • By looking at our code, we now know which elements are selected with javascript and wich are not.
    • We can change a class without breaking a javascript selector. they are not related.
    • We can change a data attribute without breaking CSS.
    Marked as helpful

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
Frontend Mentor logo

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

Oops! 😬

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

Log in with GitHub