6-1 Array function exercises

Source: Internet
Author: User

1. References
/*          var num = 10;            function show(num){                num++;                alert(num);            }            alert(num); //10            show(num);  //11  num = num            alert(num); //10*/            /*                引用            */            /*var arr = [1, 2, 3, 4];            function show(arr){                arr.push("hello");                alert(arr);            }            alert(arr); //[1, 2, 3, 4]            show(arr);  //[1, 2, 3, 4, "hello"]            alert(arr); //[1, 2, 3, 4, "hello"]*/
2. Reference sort function
3. Return
/*function add(num1, num2){                var b = num1 + num2;                return "hello";            }*/
        /*            1、确定你的函数要不要结果。            2、return语法,return后面写什么,外面拿到的值,就是return后面的表达式。        */        /*var res = add(10, 20);        alert(res);*/        
4, arguments
            function show(){                alert(arguments.length);                alert(arguments); //输出:object Arguments            }            show(1, 2, 3);            var arr = [1, 2, 3, 4, 5];            show(arr);
5, Exercise 1, have a small to large sequence of arrays. Now enter a number that requires it to be inserted into the array as it was originally. ([2,3,4,56,67,98]//632, write function map (arr) increases each digit in the array by 30% 3, writes the function has (arr, 60) to determine if there is a 60 element in the array, returns the Boolean type
        /*var arr = [2,3,4,56,67,98];        var num = 63;        for(var i = 0; i < arr.length; i++){            if(arr[i] > num){                arr.splice(i, 0, num);                break;            }        }*/        // alert(arr);        /*            关键:找出第一个第一个比该数大的数        */        var arr = [10, 20, 30, 40, 60];        function map(arr){            for(var i = 0; i < arr.length; i++){                arr[i] = arr[i] * 1.3;            }        }        /*map(arr);        alert(arr);*/        function has(arr, num){            var isHas = false; //假设没有            for(var i = 0; i < arr.length; i++){                if(arr[i] == num){                    isHas = true;                    break;                }            }            return isHas;        }        var res = has(arr, 60);        alert(res);
4. Generate 13-bit barcode (prior knowledge synthesis exercise)
EAN-13 Code rule: The 13th digit is the first 12 digits of the computed check code.                    For example: 690123456789? Third, the process of calculating its checksum code is:?                    1, the first 12 bits of odd digits and 6+0+2+4+6+8=26?                    2, the first 12 bits and 9+1+3+5+7+9=34?                    3, add odd and three times times with even and the 26+34*3=128?                    4, the result of the single digit: 128 single digit is 8?    5, use 10 minus this single digit 10-8=2?    So the check code is 2 (note: If the result of single digit is 0, then the check code is not (10-0=10), but 0) implementation Method Ean13 () Calculation Verification code, input 12-bit barcode, return the barcode with the verification code.                 For example: input: 692223361219 output: 6922233612192//Find a way to split this 12-digit number into each//69012345678 function BarCode (num) { 1. Take out each digit var Numarr = [] respectively;                    The store takes down each digit while (1) {if (num = = 0) {break;                    } numarr.push (num% 10);                num = parseint (NUM/10);                } numarr.reverse ();                2. Find odd and even and var odd = 0;                var even = 0;  for (var i = 0; i < numarr.length; i++) {if (i% 2 = = 0) {                      Odd digit odd + = Numarr[i];                    }else{//even digit even + = Numarr[i];                }}//3, var tmp = (odd + even * 3)% 10;                if (tmp! = 0) {tmp = 10-TMP;                } numarr.push (TMP);            Return number (Numarr.join (""));            } var res = BarCode (690123456789?); Alert (res);
5. Write the function norepeat (arr) to remove the repeating elements of the array and return the new array

/*
Reverse the Delete
"Note" All traverse the data to go heavy, all backwards deletes.
*/

                function noRepeat(arr){                //去重                //选择arr.length - 1个数 和后面进行比较                //i选出的数                /*for(var i = 0; i < arr.length - 1; i++){                    //j被比的数                    for(var j = i + 1; j < arr.length; j++){                        if(arr[i] == arr[j]){                            arr.splice(j, 1);                        }                    }                }*/                for(var i = arr.length - 1; i > 0; i--){                    for(var j = i - 1; j >= 0; j--){                        if(arr[i] == arr[j]){                            arr.splice(j, 1);                        }                    }                }            }            var arr = [10, 20, 20, 20, 30, 20, 40, 50];            noRepeat(arr);            alert(arr);
6. Monkeys Eat Peach

The strongest rule of kings:
1, find the critical value, the critical value is, do not calculate, direct mental arithmetic to derive the value.
2. Find out the relationship between Nth and n-1 times
3, assuming that the function can already be used, write the formula between N and N-1.

        有一堆桃子不知数目,猴子第一天吃掉一半,觉得不过瘾,        又多吃了一只,第二天照此办法,吃掉剩下桃子的一半另加一只,        天天如此,到第num(num <= 10)天早上,猴子发现只剩一只桃子了,        问这堆桃子原来有多少只?            n为还剩n天吃完的桃子数    分析:                peach(10) / 2 - 1 = peach(9);                peach(10) = (peach(9) + 1) * 2;                peach(n) = (peach(n - 1) + 1) * 2;                    function peach(n){                if(n == 1){                    return 1;                }                return (peach(n - 1) + 1) * 2;            }            //46            var res = peach(5);            alert(res);
7. Ciphertext
/*                某个公司采用公用电话传递数据,数据是四位的整数,                在传递过程中是加密的,加密规则如下:                    1、每位数字都加上5,                    2、然后用除以10的余数代替该数字,                    3、再将第一位和第四位交换,                    4、第二位和第三位交换,请编写一个函数,传入原文,输出密文            */            //4 3 2 1            function ciphertext(num){                //1、拆成数组                var numArr = [];                while(1){                    if(num == 0){                        break;                    }                    numArr.push(num % 10);                    num = parseInt(num / 10);                }                for(var i = 0; i < numArr.length; i++){                    numArr[i] += 5;                    numArr[i] = numArr[i] % 10;                }                                return Number(numArr.join(""));            }            var res = ciphertext(1234);            alert(res);
8. Calculator
【注】 <body> <!-- onclick = ""后面字符串中写着js的执行代码 --> <button id = "btn" onclick = "btnClick();">按钮</button> </body> <script> /*var oBtn = document.getElementById("btn"); oBtn.onclick = function(){ alert(1); }*/ function btnClick(){ alert("点击"); } </script>

6-1 Array function exercises

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.