JS programming Little Practice-New Ket &imweb

Source: Internet
Author: User

7-12

(1) Find the position of the element item in the given array arr:

function IndexOf (arr, item) {    var flag =-1;    for (var key in arr) {        if (arr[key] = = Item) {            flag = key;        }    }    return flag;}

(2) calculates the sum of all the elements in the given array of arr

function sum (SRR) {     var sum = 0;     for (var key in arr) {         sum + = Arr[key];     }     return sum; }

(3) Remove the array of all values in arr that are equal to item, do not directly modify the array arr, the result returns the new Year's arrays

function Remove (arr,item) {     var newArr = [];     for (var key in arr) {         if (Arr[key]) {             newarr.push (Arr[key]);         }     }     return NEWARR; }

7-13

(1) Add the element item at the end of the array arr. Do not directly modify the array arr, the result returns a new array

function Append (arr, item) {    var newarr=[];    Newarr=arr.slice (0);    Newarr.push (item);    return NEWARR;}

(2) Delete the first element of the array arr. Do not directly modify the array arr, the result returns a new array

function Curtail (arr) {    var newarr=[];    Newarr=arr.slice (0);    Newarr.shift ();    return NEWARR;}

(3) Add the element item to the index of the array arr. Do not directly modify the array arr, the result returns a new array

function Insert (arr, item, index) {    var newarr=[];    Newarr=arr.slice (0);    Newarr.splice (Index,0,item);    return NEWARR;}

Not to be continued ...

JS programming Little Practice-New Ket &imweb

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.