Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9299883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:39:43+00:00 2026-06-18T22:39:43+00:00

I have a question that relates to the question I asked here I am

  • 0

I have a question that relates to the question I asked here

I am trying to nest a loop and not quite sure if it is the way to go. What I want to do is have a number of stars displayed relevant to the score. So, if they get 3 questions they get 3 stars. The bit I am having trouble with is displaying empty stars if they haven’t reached the required score.

E.G: 10 required to get to the next level. They score 6. I can display 6 gold stars, but I am having trouble displaying 4 empty stars. I hope I explained this right.

This is the code I have so far

var se = '';
var sep = '';
for (var i = 1; i <= score; i++)
{
    se = se + '<img src="./images/star.png"/>'; 
}

I also have this loop for trying to display the empty stars

for (var j = 1; j <= i; j++)
{
    sep = sep + '<img src="./images/emptystar.png"/>';
}

I am not sure if this needs to go inside the for loop already there or outwith it. Also, for some reason it doesn’t display the correct number of empty stars. It’s displaying 1 empty for 0 correct answers and 3 or 4 for anything else

I think I have the right calculation in place in the second loop. Any pointers where I am going wrong would be much appreciated. As I said, the first loop works as it should.

Thanks everyone

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:39:44+00:00Added an answer on June 18, 2026 at 10:39 pm

    Try this:

    var stars = '';
    for (var i = 1; i <= 10; i++) {                        // Loop 10 times
        var empty = (i > score)?'empty':'';                // If i > score, set this variable to the string 'empty';
        stars += '<img src="./images/'+ empty +'star.png"/>'; // Add the image (star.png or emptystar.png depending on the score).
    }
    

    The advantage of this is that you only need 1 loop, meaning you’re not duplicating any code.

    For score == 6, this returns:

    <img src="./images/star.png"/>
    <img src="./images/star.png"/>
    <img src="./images/star.png"/>
    <img src="./images/star.png"/>
    <img src="./images/star.png"/>
    <img src="./images/star.png"/>
    <img src="./images/emptystar.png"/>
    <img src="./images/emptystar.png"/>
    <img src="./images/emptystar.png"/>
    <img src="./images/emptystar.png"/>
    

    So, how does that line var empty = (i > score)?'empty':''; work, you’re asking?
    Simple, it’s a ternary operator. Shorthand for:

    var empty;
    if(i > score){
        empty = 'empty';
    }else{
        empty = '';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a very specific question that relates to a broader question asked here:
This question may not directly relate to programming. I have noticed that the technology
I have a question that may be quite naive, but I feel the need
I have already asked this question on SuperUser but it was suggested there that
I have a question. I know that has been asked before. I looked through
This question is highly related to one that was previously asked and answered here:
This question is related to others I have asked on here, mainly regarding sorting
My question has already been asked here , but I am trying to understand
I'm sure that some of my questions may have been asked before, so please
This is an offshoot question that's related to another I asked here . I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.