JS基础学习02
2026/8/2 15:24:13 网站建设 项目流程

函数

function f1(num1, num2) // 形参(抽象) f1(10, 20) // 实参(实际值)

案例 1

function getSum(start, end){ let sum = 0 // 防止报错 if(start > end){ let temp = start start = end end = temp } // 求和 for(let i = start; i <= end; i++){ sum += i } console.log(sum) } getSum(1, 100) getSum(100, 1)

案例 2

function calculateTotalScore(studentName = []) { for (let i = 0; i < studentName.length; i++) { let sum = 0 for (let j = 1; j < studentName[i].length; j++) { sum += studentName[i][j] } console.log(`${studentName[i][0]}的总分为: ${sum}`) } } calculateTotalScore([ ['Apple', 1, 2, 3], ['Boff', 2, 3, 5], ['Cindy', 1, 5, 9], ['Daff', 5, 6, 7] ])

案例3

用户输入秒,JS自动转换时/分/秒,并补0

function calculateTotalScore(studentName = []) { for (let i = 0; i < studentName.length; i++) { let sum = 0 for (let j = 1; j < studentName[i].length; j++) { sum += studentName[i][j] } document.write(`${studentName[i][0]}的总分为: ${sum}<br>`) } } calculateTotalScore([ ['Apple', 1, 2, 3], ['Boff', 2, 3, 5], ['Cindy', 1, 5, 9], ['Daff', 5, 6, 7] ]) let second = +prompt('请输入秒数') function transTime(time_second) { let hh = parseInt(time_second / 60 / 60 % 24) let mm = parseInt(time_second / 60 % 60) let ss = parseInt(time_second % 60) // 可以在函数内实现,也可以在函数外使用return返回结果 // document.write(`转换后的时间为: ${hh < 10 ? '0' + hh : hh}:${mm < 10 ? '0' + mm : mm}:${ss < 10 ? '0' + ss : ss}<br>`) return `${hh < 10 ? '0' + hh : hh}:${mm < 10 ? '0' + mm : mm}:${ss < 10 ? '0' + ss : ss}` } document.write(`转换的时间结果为${transTime(second)}<br>`)

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询