嵌套在JavaScript中的循环 [英] Nested for loops in javascript

查看:157
本文介绍了嵌套在JavaScript中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我现在有下面的嵌套for循环
$ b $ pre $ for(var i = 0; i< = score; i ++)
{
for (var j = 0; j <= i; j ++)
{
var stsc ='< img src =./ images / star.png/>';






$ p $目标是获得stsc变量来显示星号的数量取决于分数变量的数量。

目前,无论分数的值是多少,它都只显示一颗星星。我已经尝试在嵌套循环之外添加stsc无济于事。不管我做什么,它都不会显示除了1星以外的其他东西。



你能指出我的正确方向:如何让它显示正确数量的星星(3分,如果得分是3,0分,如果得分是0等)...

感谢大家

解决方案

  var stsc =; 
var score = 0;
for(var i = 1; i <= score; i ++)
{

stsc = stsc +'< img src =./ images / star.png />';




http://jsfiddle.net/m5Btd/1295/


I was just wondering if I could get some guidance on an issue I am having with nested for loops in javascript.

I currently have the following nested for loop in place

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

The aim is to get the variable stsc to show the number of stars depending on the count of the score variable.

Currently, it will only show 1 star no matter what the value of score is. I have tried adding stsc outside of the nested loop to no avail. No matter what I do it will show nothing other than 1 star.

Could you point me in the right direction as to how to get it to show the correct number of stars (3 stars if score is 3, 0 stars if score is 0 etc...)

Thanks everyone

解决方案

var stsc="";
var score=0;
for (var i = 1; i <= score; i++)
{

        stsc = stsc +'<img src="./images/star.png"/>';

}

http://jsfiddle.net/m5Btd/1295/

这篇关于嵌套在JavaScript中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆