// 题目:函数求和
/* 思路:for循环,将形参植入i=n1,i<=n2;利用函数getSum封装循环,
最后执行return讲结果值返回给 */
function getSum(n1, n2) {
var sum = 0;
for (i = n1; i <= n2; i++) {
sum += i;
}
return sum;
}
console.log(getSum(1, 100));
/*--------------------------------------------------------*/
// 题目:求最大值
/* 思路:利用if判断,n1 > n2 ? n1return : n2 */
function match(n1, n2) {
if (n1 > n2) {
return 'match最大值为' + n1;
} else {
return 'match最大值为' + n2;
}
}
// 输出:
console.log(match(56, 35));
// 方法二:三元表达式:
function getMax(n1, n2) {
return n1 > n2 ? 'getMax最大值为' + n1 : 'getMax最大值为' + n2;
}
// 输出:
console.log(getMax(86, 43));