秒转化成小时案例
<!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>Document</title>
</head>
<body>
<script>
/*
小时: h = parseInt(总秒数 / 60 / 60 % 24)
分钟: m = parseInt(总秒数 / 60 % 60 )
秒数: s = parseInt(总秒数 % 60)
*/
// 用户输入的秒数 转换为时分秒 不足两位补0
let times = +prompt('请您输入秒数')
function getTime(time) {
let h = parseInt(time / 60 / 60 % 24)
let m = parseInt(time / 60 % 60)
let s = parseInt(time % 60)
// 补0的操作 ctrl+d 快速选中相同的内容
h = h < 10 ? '0' + h : String(h)
m = m < 10 ? '0' + m : String(m)
s = s < 10 ? '0' + s : String(s)
// s = s < 10 ? '0' + s : s.toString()
// 不做任何处理 直接返回三个值
return [h, m, s]
}
let res = getTime(times)
console.log(res);
document.write(`您输入的秒数为${time},转换的时间为:${res[0]}:${res[1]}:${res[2]}`)
</script>
</body>
</html>
求数组的最大值和最小值
<!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>Document</title>
</head>
<body>
<script>
// 封装一个 求数组的最大值和最小值的函数
function getMaxMin(arr) {
let max = min = arr[0]
for (let i = 0; i < arr.length; i++) {
max = arr[i] > max ? arr[i] : max
min = arr[i] < min ? arr[i] : min
}
return [max, min]
}
let res = getMaxMin([33, 12, 456, 0, -10])
let res2 = getMaxMin([21321, 21321, -456, 0, -10])
document.write(`最大值为${res[0]},最小值为${res[1]}`)
document.write(`最大值为${res2[0]},最小值为${res2[1]}`)
</script>
</body>
</html>
求两个数的最大值
<!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>Document</title>
</head>
<body>
<script>
// 封装一个 求两个数的最大值的函数 (函数的实参让用户输入)
function getMax(a, b) {
return a > b ? a : b
}
let num1 = +prompt('输入数字1')
let num2 = +prompt('输入数字2')
// 实参是允许传递变量
let res = getMax(num1, num2)
document.write(`您输入的数字中 较大的值为${res}`)
</script>
</body>
</html>
return返回多个值
<!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>Document</title>
</head>
<body>
<script>
// 封装一个计算两个数字的和和差的函数
function getRes(a, b) {
let he = a + b
let cha = a - b
return [he, cha]
}
let res = getRes(3, 1)
document.write(`和为${res[0]},差为${res[1]}`)
</script>
</body>
</html>