当前位置: 首页 > news >正文

面试手写第五期

文章目录

  • 一. 实现一个函数用来对 URL 的 querystring 进行编码
  • 二. 如何实现一个数组洗牌函数 shuffle
  • 三. 异步加法的几种方式
  • 四. 实现trim函数
  • 五. 求多个数组的交集
  • 六. 手写实现render函数
  • 七. 驼峰转- -转驼峰
  • 八. instanceof实现
  • 九. 组合问题
  • 十. 字符串分组

一. 实现一个函数用来对 URL 的 querystring 进行编码

const data = {a: 3,b: 4,c: 5,
};
function stringify(data) {const pairs = Object.entries(data);const qs = pairs.map(([k, v]) => {let noValue = false;if (v === null || v === undefined || typeof v === "object") {noValue = true;}return `${encodeURIComponent(k)}=${noValue ? "" : encodeURIComponent(v)}`;}).join("&");return qs;
}
// 对 data 编码后得到 querystring 如下
//=> 'a=3&b=4&c=5'
stringify(data);

二. 如何实现一个数组洗牌函数 shuffle

function shuffle(array) {let len = array.length;let _array = [...array];while (len) {let index = Math.floor(Math.random() * len--);[_array[index], _array[len]] = [_array[len], _array[index]];}return _array;
}

三. 异步加法的几种方式

串行

function add(a, b) {return Promise.resolve(a + b);
}
async function sum(arr) {let s = arr[0];for (let i = 1; i < arr.length; i++) {s = await add(s, arr[i]);}return s;
}

并行

function add(a, b) {return Promise.resolve(a + b);
}function chunk(list, size) {const l = [];for (let i = 0; i < list.length; i++) {const index = Math.floor(i / size);l[index] ??= [];l[index].push(list[i]);}return l;
}async function sum(arr) {if (arr.length === 1) return arr[0];const promises = chunk(arr, 2).map(([x, y]) =>// 注意此时单数的情况y === undefined ? x : add(x, y),);return Promise.all(promises).then((list) => sum(list));
}sum([1, 2, 3, 4]).then(res => {console.log(res);
})

四. 实现trim函数

function trim(str = "") {str = String(str);let left = 0;let right = str.length - 1;while (/\s/.test(str[left]) && left < right) {left += 1;}while (/\s/.test(str[right]) && left < right) {right -= 1;}return str.slice(left, right + 1);
}

五. 求多个数组的交集

function intersection(...args) {return args.reduce((res, cur) => [...new Set(res.filter(item => cur.includes(item)))])
}
console.log(intersection([1, 2, 2], [1, 2, 2], [1, 2]));

六. 手写实现render函数

function get(source, path, defaultValue = undefined) {// a[3].b -> a.3.b -> [a, 3, b]const paths = path.replace(/\[(\w+)\]/g, ".$1").replace(/\["(\w+)"\]/g, ".$1").replace(/\['(\w+)'\]/g, ".$1").split(".");let result = source;for (const p of paths) {result = result?.[p];}return result === undefined ? defaultValue : result;
}function render(template, data) {return template.replace(/{{\s+([^\s]+)\s+}}/g, (capture, key) => {return get(data, key);});
}

七. 驼峰转- -转驼峰

//驼峰转短横线
function toKebabCase(str) {let res = str.replace(/([A-Z])/g, (all, i) => {return "-" + i.toLowerCase();});if (res.slice(0, 1) === "-") {res = res.slice(1); //去除开头的-}return res;
}
//短横线转驼峰
function toCamelCase(str) {return str.replace(/-([a-zA-Z])/g, function (all, i) {return i.toUpperCase();});
}console.log(toCamelCase("get-element-by-id"));
console.log(toKebabCase("GetElementById"));

八. instanceof实现

function fakeInstanceOf(instance, parent) {if (typeof instance !== "object" && typeof instance !== "function") {return false;}let proto = instance?.__proto__ || null;while (true) {if (proto === null) {return false;}if (proto === parent.prototype) {return true;}proto = proto.__proto__;}
}//=> true
console.log(fakeInstanceOf([], Array));//=> true
console.log(fakeInstanceOf([], Object));//=> true
console.log(fakeInstanceOf(x => x, Object));//=> false
console.log(fakeInstanceOf('hello', Object));

九. 组合问题

function combinationGenerator(m, n) {const results = [];function backtracking(start, currentComb) {if (currentComb.length === n) {results.push(currentComb.slice());return;}for (let i = start; i <= m; i++) {currentComb.push(i);backtracking(i + 1, currentComb);currentComb.pop();}}backtracking(1, []);return results;
}
console.log(combinationGenerator(3, 2));

十. 字符串分组

function groupAnagrams(strs) {const groups = new Map();for (let str of strs) {const sortStr = str.split('').sort().join();if (!groups.has(sortStr)) {groups.set(sortStr, []);}groups.get(sortStr).push(str);}return Array.from(groups.values());
}const strings = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'];
console.log(groupAnagrams(strings));
http://www.lryc.cn/news/292311.html

相关文章:

  • 【CSS】css选择器和css获取第n个元素(:nth-of-type(n)、:nth-child(n)、first-child和last-child)
  • 解析Excel文件内容,按每列首行元素名打印出某个字符串的统计占比(超详细)
  • qt中遇到[Makfile.Debug:119:debug/app.res.o] Error 1的原因以及解决方法
  • pytorch调用gpu训练的流程以及示例
  • 学习Android的第一天
  • 回归预测 | Matlab实现CPO-LSTM【24年新算法】冠豪猪优化长短期记忆神经网络多变量回归预测
  • Typora导出html文件图片自动转换成base64
  • 『C++成长记』string使用指南
  • 硬件连通性测试:构建数字世界的无形基石
  • mysql的安装与卸载
  • 假期作业 2.2
  • 运维SRE-02 正则表达式、grep
  • 【SpringCloud】使用OpenFeign进行微服务化改造
  • DRV8313和L298N都是电机驱动,一个是驱动三相FOC无刷直流电机的,一个是驱动有刷电机,使stm32控制无刷电机简单入门知识
  • React16源码: React中event事件系统初始化源码实现
  • Qt6入门教程 15:QRadioButton
  • Json序列化和反序列化 笔记
  • 新媒体与传媒行业数据分析实践:从网络爬虫到文本挖掘的综合应用,以“中国文化“为主题
  • Visual Studio使用Git忽略不想上传到远程仓库的文件
  • Nginx简单阐述及安装配置
  • 【遥感入门系列】遥感分类技术之遥感解译
  • 解决:IDEA无法下载源码,Cannot download sources, sources not found for: xxxx
  • 什么是IDE,新手改如何选择IDE?
  • springBoot+Vue汽车销售源码
  • FPS游戏框架漫谈第五天
  • 83.如何设计高可用系统
  • Map和Set讲解
  • PHP集成开发环境 PhpStorm 2023 for mac中文激活版
  • 数学建模 - 线性规划入门:Gurobi + python
  • SpringBoot security 安全认证(二)——登录拦截器