1.倒计时效果
<script>
function countDown(time) {
var nowTime = +new Date();
var inputTime = +new Date(time);
var times = (inputTime - nowTime) / 1000;
var d = parseInt(times / 60 / 60 / 24); // 计算天数
d = d < 10 ? '0' + d : d;
var h = parseInt(times / 60 / 60 % 24) // 计算小时
h = h < 10 ? '0' + h : h;
var m = parseInt(times / 60 % 60); // 计算分数
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60); // 计算当前秒数
s = s < 10 ? '0' + s : s;
return d + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(countDown('2022-2-28 00:00:00'));
</script>
2.筛选数组:有一个包含工资的数组[1500, 1200, 2000, 2100, 1800],要求把数组中工资超过2000的删除,剩余的放到新数组里面
<script>
var arr = [1500, 1200, 2000, 2100, 1800];
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < 2000) {
newArr[newArr.length] = arr[i]; //或:newArr.push(arr[i]);
}
}
console.log(newArr);
</script>
3.数组去重:有一个数组[‘c’, ‘a’, ‘z’, ‘a’, ‘x’, ‘a’, ‘x’, ‘c’, ‘b’],要求去除数组中重复的元素。
<script>
function unique(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
return newArr;
}
var demo = unique(['c', 'a', 'z', 'a', 'x', 'a', 'x', 'c', 'b']);
console.log(demo);
</script>