简体   繁体   English

嵌套循环-Javascript

[英]Nested For Loops - Javascript

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. 因此,如果他们有3个问题,他们将获得3星。 The bit I am having trouble with is displaying empty stars if they haven't reached the required score. 我遇到的麻烦是,如果空星未达到要求的分数,则会显示它们。

EG: 10 required to get to the next level. EG:达到下一个等级需要10分。 They score 6. I can display 6 gold stars, but I am having trouble displaying 4 empty stars. 他们得到6分。我可以显示6个金星,但是我无法显示4个空星。 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. 我不确定这是否需要进入已经存在的for循环中或将其退出。 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 它显示1个空白代表0个正确答案,其余3个或4个代表

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 感谢大家

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. 这样做的好处是您只需要1个循环,这意味着您无需复制任何代码。

For score == 6 , this returns: 对于score == 6 ,将返回:

<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':''; 那么,那行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 = '';
}

try this: 尝试这个:

var stars = '';
var maxstars = 10;

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

for (var j = score; j < maxstars; j++)
{
    stars = stars + '<img src="./images/emptystar.png"/>';
}

Just use one loop: 只需使用一个循环:

var score = 5;
var sep = "";
for (var i = 0; i <= 10; i++)
{
    sep += (i < score) ? '<img src="./images/star.png"/>' : '<img src="./images/emptystar.png"/>';
}

I didn't test but that should get you started. 我没有测试,但这应该可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM