<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>内置函数初阶</title>
</head>
<body>
<style></style>
<script>
console.log(Math.PI);
console.log(Math.max(1, 99, 3));
console.log(-2, -3, -5);
console.log(Math.max(2, 2, 1, 4, 5, 'sas'));
console.log(Math.max());
var m = {
pi: 3.14,
max: function () {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
},
min: function () {
var min = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (arguments[i] < min) {
min = arguments[i];
}
}
return min;
}
}
console.log(m.pi);
console.log(m.max(25, 55, 5, 5, 847, 5, 6, 4, 6, 3, 2, 4, 7, 9, 9, 5));
console.log(m.min(2, 6, 5, 88, 3, 6, 9, 8, 5, 0, 1));
console.log('下面为内置绝对值abs');
console.log(Math.abs(-56));
console.log(Math.abs('-456') + ' 隐式转换,将字符串的数字转换成了数值型,但是输出为字符型');
console.log(Math.abs('sasa') + ' NaN means: Not a Number');
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
console.log(getRandomInt(3));
console.log(getRandomInt(100));
console.log(Math.random());
console.log('Math.random随机数这个东西,最好去 MDN 网站去搜索看看人家写成的现成的代码拿来使用');
console.log('下面是日期基础内容:');
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var dates = date.getDate();
var day = date.getDay();
var hour = date.getHours(),
minite = date.getMinutes(),
second = date.getSeconds();
var zhouci = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
console.log('现在的时间是' + year + '年' + month + '月' + dates + '日' + zhouci[day]);
console.log('时间:' + hour + '时' + minite + '分' + second + '秒');
console.log('------------------------- \n 常用案例————倒计时————分析,永远比先写代码更重要\n 使用【时间戳】');
console.log('转换公式:\n');
function jishiqi(time) {
var nowTime = +new Date(),
inputTime = +new Date(time),
times = (inputTime - nowTime) / 1000;
var d = parseInt(times / 60 / 60 / 24),
d = d < 10 ? '0' + d : d,
h = parseInt(times / 60 / 60 % 24),
h = h < 10 ? '0' + h : h,
m = parseInt(times / 60 % 60),
m = m < 10 ? '0' + m : m,
s = parseInt(times % 60);
s = s < 10 ? '0' + s : s;
return '距离2022年元旦还剩:' + d + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(jishiqi('2022-1-1 00:00:00'));
var arr = [4, 12, 3, 1];
console.log(arr[0]);
var arr1 = new Array(10),
arr2 = new Array(4, 5, 6, 9, 7, 8);
console.log(arr1 + '\n' + arr2);
console.log('数组追加之前的元素内容:\n' + arr);
arr.push(233, 'eew', 4312, 'safc');
console.log('Push追加之后的新的数组内容:\n' + arr);
arr.unshift(5, 3, 2, 'kyy')
console.log('unshift前置追加后的新数组:' + arr);
arr.pop();
console.log('pop()、shift()一次只能删除数组的最后一个、第一个元素,pop新的:' + arr);
var arr = [100, 255, 3, 45, 786, 12, 32, 14];
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < 50) {
newArr.push(arr[i]);
}
}
arr.reverse(arr); console.log('reverse翻转数组:' + arr);
arr.sort(function (a, b) {
return a - b;
});
console.log(arr);
var arr = ['red', 'bk', 'sa', 'ea', 'red'];
console.log(arr);
console.log(arr.indexOf('blue'));
function qvchong(x) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (newArr.indexOf(x[i]) === -1) {
newArr.push(x[i]);
}
}
return newArr;
}
var demo = qvchong(['a', 'b', 'c', 'd', 'e', 'a', 'a', 'a'])
console.log(demo);
console.log(demo.toString());
console.log(demo.join('-'));
charAt(index)
charCodeAt(index)
str[index]
charCodeAt()
</script>
<p>请打开控制台查看js运行的结果</p>
<br>
<div style="align-content: center;"><span>使用时间戳的倒计时原理:<br></span>
<ul>
<ol>d = parseInt(总秒数/60/60/24);</ol>
<ol>h = parseInt(总秒数/60/60%24);</ol>
<ol>m = parseInt(总秒数/60%60);</ol>
<ol>s = parseInt(总秒数%60);</ol>
</ul>
</div>
</body>
</html>
|