js面试题

面试题

给定一个数组,请输出一个随机排序的数组
 let a = [1, 2, 3, 4, 5, 6, 7];
        function shuffle(arr) {
          arr.sort(() => Math.random() - 0.5);
          return arr;
        }
        console.log(shuffle(a));
反转字符串
let str = "I am a student!";
        function reserver(str) {
          console.log(str.split("").reverse().join(""));
        }
        reserver(str);
多个参数数组找交集
      function fun(arg) {
        let args = arguments;
        let result = [];
        let flag = false;
        [...args].map((item, index) => {
          minArr = item;
          if (item.length < minArr) {
            minArr = item;
          }
        });
        minArr.map((item, index) => {
          flag = [...args].every((ite) => ite.includes(item));
          flag && result.push(item);
        });
        console.log(result);
        return result;
      }
      fun([1, 2, 3], [2, 3, 4], [2, 3], [1, 3, 5]);
手写JSON.stringfy

        if (typeof data === "string") {
          return `"${data}"`;
        } else {
          return `${data}`;
        }
      };

      const myStringfy = (params)=> {
        let resultStr = "{";
        for (let key in params) {
          resultStr += `"${key}":${stringOrOtherVal(params[key])},`;
        }
        resultStr = resultStr.slice(0, resultStr.length - 1);
        resultStr += "}";
        console.log(resultStr);
        return resultStr;
      };

      let obj1 = { nane: "李明", age: 20 };
      let obj2 = { name: "李明", age: 20, 12: function () {} };
      myStringfy([1,2,3,4,8])
      console.log(JSON.stringify([1,2,3,4,5,8]));

给一个函数 resize

  • 实现当用户缩放浏览器时,每500ms 最多只调用一次该函数
  • 实现当用户停止缩放浏览器500ms后,调用一次该函数。

      function logs() {
        console.log(1);
      }
      btn.addEventListener("click", debounce(logs, 2000), false);


      function resize(func, wait) {
        let timeout;
        return function () {
          if (!timeout) {
            timeout = setTimeout(() => {
              timeout = null;
              func.apply(this, arguments);
            }, wait);
          }
        };
      }

promise 失败三次再调用reject


      let myAxios = function (option) {
        console.log("myAxios");
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            reject("error");
          }, 1000);
        });
      };

      function runAxios(option, retry = 2) {
        let p = myAxios(option);
        if (retry > 0) {
          retry--;
          return new Promise((resolve, reject) => {
            p.then(resolve).catch(() => {
              resolve(runAxios(option, retry));
            });
          });
        }
        return p;
      }

      runAxios({
        url: "123/12",
      })
        .then(() => {
          console.log("success");
        })
        .catch((err) => {
          console.log("err");
        });

笔试题

字符串与Array.prototype.map()


      function toString(map) {
        return Object.keys(map)
          .map(_______________)
          .join("&");
      }
      console.log(toString({ a: 1, b: 2, c: 3 }))
      //a=1&b=2&c=3
      function toString(map) {
        return Object.keys(map)
          .map((item, index) => {
            return (item = `${item}=${map[item]}`);
          })
          .join("&");
      }
      console.log(toString({ a: 1, b: 2, c: 3 }))
      //a=1&b=2&c=3

字母排序 与 Array.prototype.sort()


      function sort(str) {
        return str
          .split("")
          .sort((a, b) => {
            return b.charCodeAt(0) - a.charCodeAt(0);
          })
          .join("");
      }
      console.log(sort("LeBronJames"));
      //sronmeeaLJB

      function sort(str) {
        return str
          .split("")
          .sort((a, b) => {
            return b.charCodeAt(0) - a.charCodeAt(0);
          })
          .join("");
      }
      console.log(sort("LeBronJames"));
      sronmeeaLJB

你可能感兴趣的:(js面试题)