<script>
function getmax(x, y) {
return x > y ? x : y
}
let max = getmax(1, 3)
</script>
例2:返回数组的最大值
<script>
function getArrValue(arr = []) {
let max = arr[0]
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i]
}
return max
}
let a = getArrValue([1, 5, 6, 8, 3, 2, 4, 7])
document.write(a)
</script>
例3:时间转换
<script>
let second = prompt('请输入秒数')
function getTime(t) {
d = parseInt(t / 60 / 60 / 24)
h = parseInt(t / 60 / 60 % 24)
m = parseInt(t / 60 % 60)
s = parseInt(t % 60)
if (d < 10) d = '0' + d
if (h < 10) h = '0' + h
if (m < 10) m = '0' + m
if (s < 10) s = '0' + s
document.write(`
${second}秒转换之后为
${d}天${h}小时${m}分钟${s}秒
`)
}
getTime(second)
</script>