JS---数组对象基础练习题
1.随机点名系统
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script type="text/javascript">
var isRun=true;
var a = ["彭万⾥","⾼⼤⼭","谢⼤海","马宏宇","林莽","黄强辉","章汉夫","长江","林君雄","平⼭","朱希亮","李四光","⽢铁⽣","张伍绍祖","马继祖","程孝先","宗敬先","年⼴嗣"];
var a2 = new Array();
function action(str){
var s = ElementById("bt").value;
if(s=="开始"){
isRun=true;
run();
}else{
isRun=false;
}
}
function run(){
var i = Math.floor(Math.random() * a.length+ 1)-1;
if(isRun==false){
var b =true;
for(var j in a2){
if(a2[j]==i){
b=false;
}
}
if(b){
a2[a2.length]=i;
return;
}
}
setTimeout("run()",10);
}
</script>
</head>
<body>
<div >
<div id="show" ></div>
<div >
<input id="bt" type="button" onclick="action()" value="开始"/>
</div>
</div>
</body>
</html>
2.编写函数map(arr) 把数组中的每⼀位数字都增加30%
<script>
function map(arr){
for (var i = 0; i < arr.length; i++) {
arr[i]=arr[i]*1.3;
}
console.log(arr);
}
map([10,20,30]);
</script>
3.编写函数has(arr , 60) 判断数组中是否存在60这个元素,返回布尔类型
<script>
function has(arr,x){
for (var i = 0; i < arr.length; i++) {
if (arr[i]==x) {
return true;
}
}
return false;
}
alert(has([21,324,2,12,6],60));
</script>
4.以下是某班级⼀次考试的成绩表。请计算每个学⽣总成绩,并按总成绩排名。统计各单科成绩第⼀名,输出其成绩与学号。学号 语⽂ 数学 英语 总成绩 备注
1 105 6
2 118
2 89 78 120
3 86 6
4 80
4 78 99 91
5 107.5 97 70
6 112 61 92
7 101 79 104
8 71 72 105
9 56 68 61
10 98 83 77
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
table{
border-collapse: collapse;
}
td,th{
border:1px solid #000000;
width: 100px;
height:30px;
text-align: center;
line-height: 30px;
}
</style>
sort函数 js
</head>
<body>
<script>
var data=[
{StudentNumber:1,Chinese:105,Math:62,English:118,TotalScore:"",Comment:"优"},
{StudentNumber:2,Chinese:89,Math:78,English:120,TotalScore:"",Comment:"优"},
{StudentNumber:3,Chinese:86,Math:64,English:80,TotalScore:"",Comment:"优"},
{StudentNumber:4,Chinese:78,Math:99,English:91,TotalScore:"",Comment:"优"},
{StudentNumber:5,Chinese:107.5,Math:97,English:70,TotalScore:"",Comment:"优"},    {StudentNumber:6,Chinese:112,Math:61,English:92,TotalScore:"",Comment:"优"},    {StudentNumber:7,Chinese:101,Math:79,English:104,TotalScore:"",Comment:"优"},    {StudentNumber:8,Chinese:71,Math:72,English:105,TotalScore:"",Comment:"优"},    {StudentNumber:9,Chinese:56,Math:68,English:61,TotalScore:"",Comment:"优"},
{StudentNumber:10,Chinese:98,Math:83,English:77,TotalScore:"",Comment:"优"}, ];
//总成绩求和
data.map(function (t){
t.TotalScore=t.Chinese+t.Math+t.English;
});
//总成绩排序
data.sort(function (pre,next) {
return pre.TotalScore-next.TotalScore;
});
//各单科成绩排名
data.sort(function (pre,next) {
return next.Chinese-pre.Chinese;
});
data.sort(function (pre,next) {
return next.Math-pre.Math;
});
data.sort(function (pre,next) {
return next.English-pre.English;
});
var table="<table>";
table+="<tr>";
for(var prop in data[1]){
table+="<th>"+prop+"</th>";
}
table+="</tr>";
for(var i=0;i<data.length;i++){
table+="<tr>";
for(var key in data[i] ){
table+="<td>"+data[i][key]+"</td>";
}
table+="</tr>";
}
document.write(table);
</script>
</body>
</html>
6.数字字母混合验证码 并验证
<input id="insert" type="text" placeholder="验证码" > <input id="queren" type="submit" value="确认">
<script>
/*  a--97
*  z--122
*
*  A-65
*  Z-90
*
* */
var arr=[];
//数字
for(var i=0;i<123;i++){
if(i<10){
arr.push(i);
continue;
}
//⼤写字母
if(i<65) continue;
if(i<91){
arr.push(String.fromCharCode(i));
continue;
}
//⼩写字母
if(i<97)continue;
arr.push(String.fromCharCode(i));
}
arr.sort(function () {
return Math.random()-0.5;
});
arr.length=4;
document.write(arr.join(""));
//insert=arr.innerHTML;
var ElementById("insert");
var ElementById("queren");    queren.addEventListener("click",clickHandler);
function clickHandler() {
if(insert.value==arr){
alert("正确");
} else
{alert("错误");}
}
</script>
验证有点问题求⼤神指导

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。