本文摘自PHP中文网,作者php中世界最好的语言,侵删。
这次给大家带来2018前端面试常见算法题,2018前端面的注意事项有哪些,下面就是实战案例,一起来看一下。【相关推荐:前端面试题(2020)】
1对象转换为数组
1 | var obj={ 0: '我' , 1: '的' , 2: '妈' , 3: '呀' , length:4}
|
2.统计一个字符串出现最多的字母
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function countMost(str) { const objCount = {};
str = str.split( '' ).sort().join( '' ); for (let i=0; i<str.length; i++) { let lastIndex = str.lastIndexOf(str[i]);
num = lastIndex - i + 1;
objCount[str[i]] = num;
i = lastIndex;
} let maxStr = [],
maxValue = 1; for (let p in objCount) { if (objCount[p] > maxValue) {
maxStr = [];
maxStr.push(p);
maxValue = objCount[p];
} else if (objCount[p] == maxValue){
maxStr.push(p);
}
} return maxStr.length == 1? maxStr[0] : maxStr;
}console.log(countMost( 'afjghdfffffraaaasdddddenas' ));
|
3.找出下列正数组的最大差值
1 2 3 4 5 6 | const arr = [10,5,11,7,8,9]; function getMaxProfit(arr) { let max = arr[0],
min = arr[0]; for (let i=1; i<arr.length; i++) {
max = Math.max(max,arr[i]);
min = Math.min(min,arr[i]);
} return max - min;
}console.log(getMaxProfit(arr));
|
4.获取数组中最大或者最小值
1 2 3 4 | function maxAndMin(arr){ return { max:Math.max.apply(null,arr.join( ',' ).split( ',' )), min:Math.min.apply(null,arr.join( ',' ).split( ',' ))
}
} var arr = [22,0,[3,4,2,55]];
maxAndMin(arr).max;
|
5.生成指定长度的随机字母数字字符串
1 2 | function getRandomStr(len) { var str = "" ; for ( ; str.length < len; str += Math.random().toString(36). substr (2)); return str. substr (0, len);
}
|
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
相关阅读:
用jq发送多个ajax然后执行回调的小技巧
怎样使用伪元素first-letter让文字首字母大写
JavaScript的函数重载详解
以上就是2018前端面试常见算法题的详细内容,更多文章请关注木庄网络博客!
相关阅读 >>
如何设计算法?常见的算法范式介绍
深入浅析javascript中的快速排序
javascript算法之归并排序算法(详解)
带你轻松理解kmp算法
2018前端面试常见算法题
javascript深度优先遍历(dfs)和广度优先遍历(bfs)算法的介绍
更多相关阅读请进入《2018》频道 >>
人民邮电出版社
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者
转载请注明出处:木庄网络博客 » 2018前端面试常见算法题