Home  >  Article  >  Web Front-end  >  Summary of front-end algorithm interview questions

Summary of front-end algorithm interview questions

php中世界最好的语言
php中世界最好的语言Original
2018-05-24 11:17:571791browse

这次给大家带来前端算法面试题汇总,使用前端算法的注意事项有哪些,下面就是实战案例,一起来看一下。

数字千分位格式化

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中文网其它相关文章!

推荐阅读:

js原型使用详解

React结合TypeScript和Mobx步骤详解

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn