JS——function exercise (with source code)

Table of contents

Function definition:

Function: It is **encapsulating a code block that can be called repeatedly**. This code block can **reuse a large amount of code**.

Exercises: 1: Create a function that calculates the sum of two numbers.      

 2: Encapsulate a function of the difference between two numbers.        

    3: Create a function that calculates the maximum of two numbers.        

   4: Create a function that passes an array and returns the maximum, minimum, and sum in the array.        

5: Pass an array to realize the function of flipping the array.      

  6: Encapsulate a function: the browser receives a month to judge the number of days in the month.      

7: Make a simple calculator, the browser receives +,-*/, receives two numbers, and then calculates the result.        

8: Encapsulate a function to determine whether a certain value exists in an array.        


Function definition:

Function: It is **encapsulating a code block that can be called repeatedly**. This code block can **reuse a large amount of code**.

Exercises:
  1: Create a function that calculates the sum of two numbers.
      

     function sum(num1, num2) {
                return num1 + num2;
            }
            var sum = sum(1, 2);
            console.log(sum);

 2: Encapsulate a function of the difference between two numbers.
        

    function getDiff(num1, num2) {
                return num1 - num2;
            }
            ver diff = getDiff(1, 2);
            console.log(diff);

    3: Create a function that calculates the maximum of two numbers.
        

    function getMax(num1, num2) {
                if (num1 > num2) {
                    return num1;
                } else {
                    return num2;
                }
                return num1 > num2 ? num1 : num2;
            }
            console.log(getMax(80, 55));


   4: Create a function that passes an array and returns the maximum, minimum, and sum in the array.  
      

      var numArr = [10, 3, 11, 2, 20];

            function getValue(arr) {
                var numArr = [];
                var max = arr[0];
                var min = arr[0];
                var sum = 0;
                for (var i = 0; i < arr.length; i++) {
                    if (max < arr[i]) {
                        max = arr[i];
                    }
                    if (max < arr[i]) {
                        min = arr[i];
                    }
                    sum += arr[i];
                }
                numArr[0] = max;
                numArr[1] = min;
                numArr[2] = sum;
                return numArr;
            }
            var newarr = getValue(numArr);
            console.log(max + min + sum)

5: Pass an array to realize the function of flipping the array.
      

      function reverse(arr) {
                var newarr = [];
                for (var i = arr.length - 1; i >= 0; i--) {
                    newarr[newarr.length] = arr[i];
                }
                return newarr
            }
            var re = reverse([1, 2, 3, 4, 5, 6])
            console.log(re);

  6: Encapsulate a function: the browser receives a month to judge the number of days in the month.
      

      function newday() {
                var month = prompt("请输入月份:");
                var day;
                switch (Number(month)) {
                    case 1:
                        day = 31;
                        break;
                    case 2:
                        day = 28;
                        break;
                    case 3:
                        day = 31;
                        break;
                    case 4:
                        day = 30;
                        break;
                    case 5:
                        day = 31;
                        break;
                    case 6:
                        day = 30;
                        break;
                    case 7:
                        day = 31;
                        break;
                    case 8:
                        day = 31;
                        break;
                    case 9:
                        day = 30;
                        break;
                    case 10:
                        day = 31;
                        break;
                    case 11:
                        day = 30;
                        break;
                    case 12:
                        day = 31;
                        break;
                    default:
                        console.log("输入错误");
                }
                return day;
            }
            var day = newday();
            console.log('这个月有' + day + '天');

7: Make a simple calculator, the browser receives +,-*/, receives two numbers, and then calculates the result.
        

    function getSum() {
                var first = Number(prompt("请输入第一个数字:"));
                var symbol = prompt("请输入运算符:");
                var second = Number(prompt("请输入第二个数:"));
                var res;

                if (symbol == "+") {
                    res = first + second;
                }
                if (symbol == "-") {
                    res = first - second;
                }
                if (symbol == "*") {
                    res = first * second;
                }
                if (symbol == "/") {
                    res = first / second;
                }
                return res;
            }
            var res = getSum();
            console.log(res);


8: Encapsulate a function to determine whether a certain value exists in an array.
        

    var arr = [1, 10, 78, 90, 67]; //定义一个数组
            function findItem(item, arr) { //封装一个函数
                var flag = false; //默认这个数不在数据里面
                //业务逻辑
                for (var i = 0; i < arr.length; i++) {
                    if (item == arr[i]) { //如果找到了这个数
                        flag = true;
                    }
                }
                return flag;
            }
            //调用函数
            var flag = findItem(1, arr);
            console.log(flag);

Guess you like

Origin blog.csdn.net/m0_72975897/article/details/127072631