显示单词的代码,JS Jquery

时间:2014-10-25 08:25:10

标签: javascript jquery

代码链接:http://jsbin.com/lozifokuzi/1/edit?html,js,output应该显示16个单词,但它只显示15个单词(用希伯来语写的单词)。 代码用JavaScript和jQuery语言编写。

$(document).ready(function () {
// creat array of objects, DetermineIDs
var words = new Array(16);
for (var i = 0; i < words.length; i++) {
    words[i] = new Object();
    words[i].id = i + 1;
}
                
//insert into objects words
               
words[0].word = "קוף";
words[1].word = "קוף";
words[2].word = "אריה";
words[3].word = "אריה";
words[4].word = "נמר";
words[5].word = "נמר";
words[6].word = "טלפון";
words[7].word = "טלפון";
words[8].word = "מחשב";
words[9].word = "מחשב";
words[10].word = "מקלדת";
words[11].word = "מקלדת";
words[12].word = "אוגר";
words[13].word = "אוגר";
words[14].word = "עכבר";
words[15].word = "עכבר";
//Determine locations
                
                var ret=Random(loc);
                
                var random = 0;
                for (var i = 0; i < words.length; i++) {
                    words[i].loca=ret[0];
                    loc=ret[1];
                    ret = Random(loc);
                   
                          }
                
                
                    
              //write the words
                
                for (var i = 0; i < 16; i++) {
                    
                    $("#c" + (words[i].loca)).html(words[i].word);
                }
                
            });
            function RandomC(ezer, random) {

                for (var i = 0; i <= 16; i++) {
                    if (ezer[i] == random) {

                        return true;


                    }
                }
                return false;
            }
            function Random(lq) {
                var ezer = new Array(16);
                for (var i = 0; i < 16; i++) {
                    ezer[i] = lq[i];
                }

                var random = 0;
                while ((random < 1 || random > 17) || RandomC(ezer, random)) {

                    random = parseInt(Math.random() * 100);
                }


                for (var i = 0; i < lq.length; i++) {
                    if (lq[i] == null) {
                        ezer[i] = random;
                        break;
                    }
                }
                var arr = new Array(2);
                arr[0] = random;
                arr[1] = ezer;
                return arr;
            }
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <title></title>
    
</head>
<body>
    <article>
        <div id="l1">
            <p id="c1"></p>
            <p id ="c2"></p>
            <p id="c3"></p>
            <p id="c4"></p>
        </div>
        <div id="l2">
            <p id="c5"></p>
            <p id="c6"></p>
            <p id="c7"></p>
            <p id="c8"></p>
        </div>
        <div id="l3">
            <p id="c9"></p>
            <p id="c10"></p>
            <p id="c11"></p>
            <p id="c12"></p>
        </div>
        <div id="l4">
            <p id="c13"></p>
            <p id="c14"></p>
            <p id="c15"></p>
            <p id="c16"></p>
        </div>
    </article>
</body>
</html>

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

你在函数Random()中有17个循环:

   while ((random < 1 || random > 17) || RandomC(ezer, random)) 
   {
      random = parseInt(Math.random() * 100);
   }

使它成为16:

   while ((random < 1 || random > 16) || RandomC(ezer, random)) 
   {
      random = parseInt(Math.random() * 100);
   }

你的问题是你有16个元素从1到16,你的随机函数给出16个随机数从1到17,如果返回范围有数字17它缺少从1到16的东西,这意味着你的p元素那个nubmer没有充满内容。