1.题目描述:数组的首尾两个元素互换let arr =['aa','bb','cc','dd'];
let f = arr=>{
let f = arr[0];
let l = arr[arr.length-1];
arr[0] = l;
arr[arr.length-1] = f;
console.log(arr);
}
f(arr);
2.随机选学员
从以下学员名单中随机选出4个学员:
var arr = ["鹿晗","王俊凯","蔡徐坤","彭于晏","周杰伦","刘德华","赵本山"];
注意:不要有重复的学员
let arr = ["鹿晗","王俊凯","蔡徐坤","彭于晏","周杰伦","刘德华","赵本山"];
let arr1 =[];
const getRanom =(min,max)=>{
return Math.floor(Math.random()*(max-min+1)+min);
}
for (let i =0;i<4;i++){
arr1.push(arr[getRanom([0],arr.length-1)]);
}
console.log(arr1)
3.随机排序
对以下学员随机排序,生成一个新数组:
var arr = ["鹿晗","王俊凯","蔡徐坤","彭于晏","周杰伦","刘德华","赵本山"];
let arr = ["鹿晗","王俊凯","蔡徐坤","彭于晏","周杰伦","刘德华","赵本山"];
function shuffle(arr) {
let i = arr.length;
while (i) {
let j = Math.floor(Math.random() * i--);
[arr[j], arr[i]] = [arr[i], arr[j]];
}
return arr;
}
console.log(shuffle(arr))
题目描述:制作一个函数,getDayNum( 年月日日期 ),可以返回指定日期是当前年的第几天 启动时自动显示当日是当年的第多少天。
let date = new Date(); let year = date.getFullYear(); let month = date.getMonth()+1; let dates = date.getDate(); let nowTime = year+'-'+month+'-'+dates; const getDayNum = time =>{ let begin = time; console.log(begin) let nowYear = time.substr('0','4'); let firstTime = +new Date( nowYear+'-1-1'); let inputTime = +new Date(time); let times = (inputTime-firstTime)/1000; let d = parseInt(times/60/60/24 +1); alert(begin+"是"+nowYear+'年的第'+d+'天'); getDayNum(prompt('输入要查询的日期,并以‘-’间隔')) ; } getDayNum(nowTime); getDayNum(prompt('输入要查询的日期,并以‘-’间隔'));
题目描述:使用Math对象,制作一个16进制的随机颜色
const getRandomColor =()=>{
let arr = [0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
const getColor =(min,max)=>Math.floor(Math.random()*(max-min+1) + min);
let color = '';
for (let i =0;i<5;i++){
color += arr[getColor(0,arr.length-1)];
}
return '#'+color ;
}
console.log(getRandomColor());
字符串:“abaasdffggghhjjkkgfddsssss3444343”,问题如下:
1、 字符串的长度
console.log(str.length);
2、 取出指定位置的字符,如:0,3,5,9等
let str0 = str.charAt(0);
3、 查找指定字符是否在以上字符串中存在,如:i,c ,b等
let index = str.indexOf('j'); let num =0; while (index !== -1){ console.log('j出现的位置是'+index); num++; index = str.indexOf('j',index+1) } console.log('j出现的次数是'+num)
4、 替换指定的字符,如:g替换为22,ss替换为b等操作方法
let str ='abaasdffggghhjjkkgfddsssss3444343'; while (str.indexOf('j')!== -1){ str = str.replace('j','2'); } console.log(str)
5、 截取指定开始位置到结束位置的字符串,如:取得1-5的字符串
let substr = str.substr(0,5)
6、 找出以上字符串中出现次数最多的字符和出现的次数
let str ='abaasdffggghhjjkkgfddsssss3444343'; let o ={} for (let i =0;imax){ max = o[key]; zd = key; } } console.log('出现次数最多的是'+zd,'出现了'+max+'次')
7、 遍历字符串,并将遍历出的字符两头添加符号“@”
let str ='abaasdffggghhjjkkgfddsssss3444343';
for (let i=0;ilet str1 ='@'+ str.charAt(i)+'@';
console.log(str1)
}