var num=6; function Girl(beautifulScore){ this.beautifulScore=beautifulScore; } var girls=[]; for (var i = 0; i < num; i++) { girls[i]=new Girl(Math.floor(Math.random()*40+60)); //Math.floor(Math.random()*40+60得到一个60-99的数值,JS没有PHP的range()这样的函数 } girls.sort(function(girl1,girl2){ //排序操作,sort(a,b)传入数组中相邻两个元素,通过这个元素的某个属性大小来进行排序,如果要升序则return a-b;降序则return b-a return girl2.beautifulScore-girl1.beautifulScore; }); for (var i = 0; i < num; i++) { if (i===0) { var wife=girls[i]; console.log(wife.beautifulScore); }else{ eval(‘var concubine‘+i+‘=girls[i];console.log(concubine‘+i+‘.beautifulScore)‘); //要生成a1,a2,a3,a4这样的变量名,则需要用eval()方法 } }
时间: 2024-10-18 05:47:44