Home > Web Front-end > JS Tutorial > body text

Summary of front-end algorithm interview questions

php中世界最好的语言
Release: 2018-05-24 11:17:57
Original
1810 people have browsed it

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

数字千分位格式化

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));
Copy after login

字符串中出现次数最多的字母

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}`);
Copy after login

深度优先遍历树结构

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);
Copy after login

相信看了本文案例你已经掌握了方法,更多精彩请关注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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template