前端常见面试题(二十一)@郝晨光
2019-08-07 本文已影响150人
郝晨光
URL转对象
let url = 'https://www.baidu.com?name=郝晨光&age=24&sex=男';
function getURLParams(url) {
let paramsStr = url.split('?')[1];
let paramsArr = paramsStr.split('&');
let result = {};
for(let i = 0; i < paramsArr.length; i++) {
let params = paramsArr[i].split('=');
result[params[0]] = params[1];
}
return result;
}
console.log(getURLParams(url));
不使用循环返回数组
let num = 10;
function getArr(num) {
let arr = [];
return (function(){
arr.unshift(num);
num--;
if(num > 0) {
arguments.callee();
}
return arr;
})()
}
console.log(getArr(num));
事件委托的实现
<body>
<ul id="my-ul">
<li>
<button class="btn">哈哈</button>
</li>
</ul>
</body>
<script>
let oUl = document.getElementById('my-ul');
oUl.onclick = function(event) {
event = event || window.event;
let className = event.target.className;
switch (className) {
case 'btn':
alert('btn click');
break;
default:
break;
}
}
</script>
冒泡排序
let arr = [3,21,5,6,3,2,4,7,3,46,8,43];
function sort(arr) {
for(let i = 0; i < arr.length; i++) {
for(let j = i + 1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
sort(arr);
console.log(arr);
输出字符串中出现次数最多的字符及其出现次数
let str = 'asdfghjasdfghjassdasasdasdaa';
function getMaxWord(str) {
let obj = {};
let result = {
word: '',
count: 0
};
for(let i = 0; i < str.length; i++) {
if(obj[str[i]]) {
obj[str[i]]++;
}else {
obj[str[i]] = 1;
}
}
for(let i in obj) {
if(result.count < obj[i]) {
result.word = i;
result.count = obj[i];
}
}
return result;
}
console.log(getMaxWord(str));
判断是否是回文
let str = '我爱妈妈,妈妈爱我';
function palindrome(str) {
return str === str.split('').reverse().join('');
}
console.log(palindrome(str));
千分符,将数字格式化成RMB格式
let rmb = 1234567890;
function toRMB(num) {
let str = num.toString();
let arr = str.split('').reverse();
let result = [];
for(let i = 0; i < arr.length; i ++) {
if(i % 3 === 0 && i !== 0) {
result.push(',')
}
result.push(arr[i]);
}
return result.reverse().join('');
}
console.log(toRMB(rmb));
字符串转驼峰
let str = 'get-element-by-id';
function toHumpName(str) {
return str.replace(/\-\w/g, ([,word]) => {
return word.toUpperCase()
})
}
console.log(toHumpName(str));
找出数组最大的差值,最大值减最小值的差
let arr = [1, 2, 5, 65, 90, 101, 32, 54, 43, 53];
function maxDifference(arr) {
let min = Math.min.apply(null, arr);
let max = Math.max.apply(null, arr);
return max - min;
}
console.log(maxDifference(arr));
数组去重
let arr = [1, 2, 3, 4, 5, 1, 2, 3, 5, 6, 7, 8, 1, 5];
function noRepeat(arr) {
let result = [];
for(let i = 0; i < arr.length; i++) {
if(!result.includes(arr[i])) {
result.push(arr[i]);
}
}
return result;
}
console.log(noRepeat(arr));
获取非行间样式
function getStyle(el, attr) {
return el.current? el.currrent[attr] : getComputedStyle(el)[attr];
}
斐波那契数列
function fbnq(n) {
return n < 2 ? n : arguments.callee(n - 1) + arguments.callee(n - 2);
}
function fbnq2(n) {
let num1 = 1;
let num2 = 1;
let result = num2;
for(let i = 2; i < n; i++) {
result = num1 + num2;
num1 = num2;
num2 = result;
}
return n < 2 ? n : result;
}
let arr = [];
let arr2 = [];
for(let i = 0; i <= 10; i++) {
arr.push(fbnq(i));
arr2.push(fbnq2(i));
}
console.log(arr);
console.log(arr2);
如果本文对您有帮助,可以看看本人的其他文章:
前端常见面试题(二十)@郝晨光
前端常见面试题(十九)@郝晨光
前端常见面试题(十八)@郝晨光