这次给大家带来前端算法面试题汇总,使用前端算法的注意事项有哪些,下面就是实战案例,一起来看一下。
function format (num) { let [integer,decimal]=String(num).split('.'); let regObj=/\d{1,3}(?=(\d{3})*$)/g; let arr=String(integer).match(regObj); return arr.join(',')+(typeof decimal=="undefined"?"":'.'+decimal); } console.log(format(1234567890.2323));
let str = "zhaochucichuzuiduodezifu"; str = str.split("").sort().join(""); let maxLen=0; let match=null; let key=""; let regExp=/(\w)\1*/g; while (match =regExp.exec(str) ){ if (match[0].length>maxLen){ maxLen=match[0].length; key=match[1]; } } console.log(`key:${key},count:${maxLen}`);
var data={ key1:"str1", key2:{ key3:"key3", key4:"key4", key5:{ key6:"key6" } } } function treeTraversal(data) { function f(prefix,data) { var keys=Object.keys(data); if(!/^\s*$/.test(prefix)){ prefix+=" "; } keys.forEach((item,index)=>{ if (typeof data[item] == "string"){ console.log(prefix+item+" "+data[item]); return prefix+item+" "+data[item]; }else{ prefix+=item; return f(prefix, data[item]); } }); } f("",data); } treeTraversal(data);
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of Summary of front-end algorithm interview questions. For more information, please follow other related articles on the PHP Chinese website!