1.数组函数
var arr1 = new Array('张居正','严嵩','严世潘','徐阶');
var str = arr1.pop();
console.log(str, arr1);
arr1.shift();
console.log(arr1);
var arr2 = new Array('戚继光','李成梁','李如松','海瑞');
var arr3 = arr2.splice(2,1,'努尔哈赤','冯保');
console.log(arr3,arr2);
var arr4 = ['李舜臣','李如松','裕仁天皇'];
arr4.splice(1);
console.log(arr4);
var arr5 = ['李成梁','李成梁','戚继光','李如松','戚继光','海瑞'];
console.log(arr5.indexOf('海贼王'));
console.log(arr5.indexOf('李成梁',2));
// 找到所有'戚继光'所在的下标,放入到新的数组中
var arr6 = [];
console.log('戚继光', arr5.indexOf('戚继光'));
for(var i=0;arr5.indexOf('戚继光',i)!=-1;){
var temp = arr5.indexOf('戚继光',i);
arr6.push(temp);
i = temp+1;
}
console.log(arr6);
var arr7 = ['灵明石猴','赤尻马猴','通臂猿猴','六耳猕猴'];
var arr8 = arr7.slice(1,2);
console.log(arr7,arr8);
var arr9 = arr7.slice(-4,-1);
console.log(arr7,arr9);
var arr10 = arr7.slice('a',-2);
console.log(arr7,arr10);
console.log('**********');
var arr11 = arr7.slice();
console.log(arr7,arr11);
2.数组函数
var arr1 = [5,6,7,8,9,10];
var bol1 = arr1.every(function(item){
// 在函数中,添加设置的条件,item指的是数组中的每一个元素
return item>4;
});
console.log(bol1);
var bol2 = arr1.some(function(aa,index){
console.log(index);
return aa==10;
});
console.log(bol2);
var arr2 = [1,2,3,4,5,6,7,8,9,10];
var arr3 = arr2.filter(function(ele){
return ele%2==0;
});
console.log(arr2,arr3);
var arr4 = arr2.map(function(ele){
return ele+'px';
});
console.log(arr2,arr4);
var arr5 = ['珍珍','爱爱','怜怜'];
var arr6 = ['骊山老母','观音菩萨','零基普斯'];
var arr7 = ['高翠兰'];
var arr8 = arr5.concat(arr6,arr7);
console.log(arr8);
var str1 = arr5.join('==');
document.write(str1);
var str2 = '万里%长城%万里%梦';
var arr9 = str2.split('%');
console.log(arr9);
arr5.reverse();
console.log(arr5);
// 练习,把str3倒叙输出
var str3 = '百世英雄百世空';
var arr = str3.split('');
arr.reverse();
var str4 = arr.join('');
document.write('<hr/>'+str4);
var arr11 = [1,0,3,13,2,24,33,14,15];
// arr11.sort();
// console.log(arr11);
arr11.sort(function(num1,num2){
return num2-num1;
});
console.log(arr11);
var arr12 = [1,0,3,13,2,24,33,14,15];
arr12.sort(function(){
return 0;
});
console.log(arr12);
var arr12 = [1,2,3,4,5,6,7,8,9];
//数组打乱,sort() 函数中如果返回值<0,则交换两个元素的位置,如果返回值>=0,不交换位置
arr12.sort(function(){
return Math.random()-0.5;
});
console.log(arr12);
3.冒泡排序
var arr = [1,2,3,4,5];
//执行第一趟比较,比较4次, 把5放在了最后面
for(var i=0;i<4;i++){
if(arr[i]>arr[i+1]){
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
console.log(arr); // 3 4 1 2 5
//执行第二趟比较,比较3次, 把4 5放在了最后面
for(var i=0;i<3;i++){
if(arr[i]>arr[i+1]){
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
console.log(arr); // 3 1 2 4 5
//执行第三趟比较,比较2次, 把3 4 5放在了最后面
for(var i=0;i<2;i++){
if(arr[i]>arr[i+1]){
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
console.log(arr); // 1 2 3 4 5
//执行第四趟比较,比较1次, 把2 3 4 5放在了最后面
for(var i=0;i<1;i++){
if(arr[i]>arr[i+1]){
var temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
console.log(arr); // 1 2 3 4 5
var arr1 = [23,45,1,23,45,56,7,6,8,0,34,65];
//冒泡排序的写法
for(var i=1;i<arr1.length;i++){ // 外层控制趟数
for(var j=0;j<arr1.length-i;j++){
if(arr1[j]>arr1[j+1]){
var temp = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = temp;
}
}
}
console.log(arr1);
4.十五二十小游戏
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>十五二十</title>
</head>
<body>
<div class="girl">
美女:
<input type="text" id="chu" value="" placeholder="出的数" readonly="readonly" />
<input readonly="readonly" type="text" id="han" value="" placeholder="喊的数" />
</div>
<div class="boy">
帅哥出:
<select id="boyChu">
<option value="0">0</option>
<option value="5">5</option>
<option value="10">10</option>
</select>
帅哥喊:
<select id="boyHan">
<option value="0">0</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
</div>
<button type="button" id="btn">开始</button>
</body>
</html>
<script type="text/javascript">
var chu = document.getElementById("chu"); //美女出
var han = document.getElementById("han"); //美女喊
var boyChu = document.getElementById("boyChu"); //帅哥出
var boyHan = document.getElementById("boyHan"); //帅哥喊
var btn = document.getElementById("btn");
var arr1 = [0,5,10]; //出的数组
var arr2 = [0,5,10,15,20]; //喊的数组
btn.onclick = function(){
// 随机美女出的数下标
var index1 = Math.floor(Math.random()*3);
// 随机美女喊的数下标
var index2 = Math.floor(Math.random()*5); //3
// 设置喊的数不小于出的数
while(arr2[index2]<arr1[index1]){
var index1 = Math.floor(Math.random()*3);
var index2 = Math.floor(Math.random()*5);
}
//美女出的数
var num1 = arr1[index1];
//美女喊的数
var num2 = arr2[index2]; //15
//给元素赋值
chu.value = num1;
han.value = num2;
//帅哥出的数
var num3 = boyChu.value-0; //转化为数字
//帅哥喊的数
var num4 = boyHan.value-0; //转化为数字
// 两人出的数综合
var sum = num1+num3;
//判断
if(num2==sum && num4==sum){
alert('平局');
}else if(num2==sum){
alert('美女赢');
}else if(num4==sum){
alert('帅哥赢');
}else{
alert('再来一局');
}
//code
};
</script>