1. // 题目:函数求和
    2. /* 思路:for循环,将形参植入i=n1,i<=n2;利用函数getSum封装循环,
    3. 最后执行return讲结果值返回给 */
    4. function getSum(n1, n2) {
    5. var sum = 0;
    6. for (i = n1; i <= n2; i++) {
    7. sum += i;
    8. }
    9. return sum;
    10. }
    11. console.log(getSum(1, 100));
    12. /*--------------------------------------------------------*/
    13. // 题目:求最大值
    14. /* 思路:利用if判断,n1 > n2 ? n1return : n2 */
    15. function match(n1, n2) {
    16. if (n1 > n2) {
    17. return 'match最大值为' + n1;
    18. } else {
    19. return 'match最大值为' + n2;
    20. }
    21. }
    22. // 输出:
    23. console.log(match(56, 35));
    24. // 方法二:三元表达式:
    25. function getMax(n1, n2) {
    26. return n1 > n2 ? 'getMax最大值为' + n1 : 'getMax最大值为' + n2;
    27. }
    28. // 输出:
    29. console.log(getMax(86, 43));