# 18-数组练习

01.翻转数组

  <script>
    //需求:求一个任意数组的反转数组

    // 写法一:反向遍历
    function getReverse() {
      let arr = [];
      for (let i = arguments.length - 1; i >= 0; i--) {
        arr[arr.length] = arguments[i];
      }
      return arr;
    }
    let res = getReverse(1, 2, 3, 4, 5);
    console.log(res);//[5, 4, 3, 2, 1]

    // 写法二:变量交换
    function getReverse2() {
      let arr2 = [...arguments];

      // 只需要循环数组长度的一半即可喔
      for (let i = 0; i < arr2.length / 2; i++) {
        let temp;
        // 将[i]存在temp中
        temp = arr2[i];
        // 把最后一个i存到i中
        arr2[i] = arr2[arr2.length - 1 - i];
        // 在把[i]给最后一个i
        arr2[arr2.length - 1 - i] = temp;
      }
      return arr2;
    }

    let res2 = getReverse2(1, 2, 3, 4, 5);
    console.log(res2);// [5, 4, 3, 2, 1]

    // 写法三:用reverse()
    function getReverse3() {
      let arr3 = [...arguments];
      return arr3.reverse();
    }
    let res3 = getReverse3(1, 2, 3, 4, 5);
    console.log(res3); //[5, 4, 3, 2, 1]
  </script>

02.最大值

  <script>
    // 需求:返回任意数组的最大值

    // 写法一:假设法
    function getMax() {
      let max = arguments[0];
      for (let i = 0; i < arguments.length; i++) {
        max = max > arguments[i] ? max : arguments[i];
      }
      return max;
    }
    let res = getMax(1, 2, 3, 4, 5, 6, 9)
    console.log(res);

    // 写法二:排序
    function sortFunc(a, b) {
      return a - b;//升序
    }

    function getMax2() {
      let arr = [...arguments].sort(sortFunc);
      // 最后一个就是最大值
      let max = arr[arr.length - 1];
      return max;
    }
    let res2 = getMax2(88, 44, 2, 0, 55);
    console.log(res2);


    // 写法三:es6
    function getMax3() {
      return Math.max(...arguments);
    }
    let res3 = getMax3(6, 44, 8, 123);
    console.log(res3);
  </script>

03.数组去0

  <script>
    // 要求将数组中的0项去掉,将不为0的的值存入到新的数组 生成新的数组

    // 方法一、
    function removeZero() {
      let arr = [];
      for (let i = 0; i < arguments.length; i++) {
        // 等于0就跳过
        if (arguments[i] === 0) {
          continue;
        }
        // push进去
        arr.push(arguments[i]);
      }
      return arr;
    }
    let res = removeZero(1, 2, 3, 0, 4, 5, 6)
    console.log(res);

    // 方法二
    function removeZero2() {
      let arr2 = [...arguments];
      // 筛选出不为0的并返回
      return arr2.filter(item => { return item !== 0 });;
    }
    console.log(removeZero2(1, 2, 3, 0, 1, 2, 3, 0));
  </script>

04.返回0的索引和次数

  <script>

    // 需求:返回数组中0出现的次数及索引值
    function getZero() {
      let arr = [...arguments],
        arr1 = [],
        num = 0;
      for (let i = 0; i < arr.length; i++) {
        // 查询0
        if (arr[i] === 0) {
          //当arr[i]等于0时,查询arr[i]在数组中的位置
          //然后从当前位置继续往后面查找
          arr1.push(arr.indexOf(arr[i], i));
          //次数+1
          num++;
        }
      }
      return ['索引' + arr1, '次数' + num];
    }

    let res = getZero(1, 2, 3, 0, 1, 5, 0);
    console.log(...res.flat());//拉平二维数组

  </script>

05.数组去重

  <script>
    // 去除数组中的重复项
    // 方法一:includes
    function removeSame() {
      let arr = [...arguments];
      let arr1 = [];
      for (var i = 0; i < arr.length; i++) {
        // 如果不包含就加进去
        if (!arr1.includes(arr[i])) {
          arr1.push(arr[i]);
        }
      }
      return arr1;
    }
    console.log(removeSame(1, 0, 2, 3, 4, 0, 1, 5, 3));


    // 方法二、indexOf
    function removeSame1() {
      let arr = [...arguments];
      let arr2 = [];
      // 遍历
      arr.forEach(item => {
        // arr1里面没有item,就加进去
        if (arr2.indexOf(item) === -1) {
          arr2.push(item);
        }
      })
      return arr2;
    }
    let res2 = removeSame1(1, 2, 3, 4, 5, 4, 2, 3, 8);
    console.log(res2);
  </script>

猜你喜欢

转载自www.cnblogs.com/xiaoaitongxue/p/12748213.html