心态放松,精神饱满;沉淀自己,善于学习;知识在于积累,更是一笔财富。
✨ ⭐️ 看完文章,感谢各位支持一下呗!
const getArr = (startNum,endNum) =>{
let arr = [];
for(i=startNum;i<=endNum;i++){
arr.push(i)
}
return arr;
}
console.log("1.从0生成到指定值的数字数组",getArr(1,2));
输出:[ 1, 2 ]
const getArrMaxVal = (arr)=>{
return Math.max(...arr);
}
console.log("2.获取数组的最大值",getArrMaxVal([0,1,3,4,5,10]));
输出: 10
const getArrMinVal = (arr)=>{
return Math.min(...arr);
}
console.log("3.获取数组的最小值",getArrMinVal([0,1,3,4,5,10]));
输出: 0
const removeArrEqual = (arr)=>{
const result = arr.filter((item,index,self)=>{
// indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置
// console.log(self.indexOf(item))0113455
return self.indexOf(item) === index;
})
return result;
}
console.log("4.数组去重1",removeArrEqual([1,2,2,3,4,5,5]));
输出: [ 1, 2, 3, 4, 5 ]
const removeEqualSuper = (arr)=>{
const setArr = new Set(arr);
const result = Array.from(setArr);
return result;
}
console.log("4.数组去重2",removeEqualSuper([1,2,2,3,4,5,5]));
输出:[ 1, 2, 3, 4, 5 ]
斐波那契数列,又称黄金分割数列
像0,1,1,2,3,5,8…
在数学上,斐波那契数列被一下递推的方式定义:F(0)=0,F(1)=1,F(n)=F(n-1)+F(n-2)(n>=2,n∈N*)
用文字来说,就是斐波那契数列由 0 和 1 开始,之后的斐波那契数列系数就由之前的两数相加。
常用的计算斐波那契数列的方法分为两大类:循环和递归。
const fibonacci = (n)=>{
let fibArr = [];
let i =0;
while(i
输出: [0, 1, 1, 2,3, 5, 8]
求斐波那契数列0,1,1,2,3,5,8,13,21,34…第n项
const fib = (num)=>{
if(num<2){
return num;
}
return fib(num-1)+fib(num-2);
}
console.log("6.递归方法求斐波那契数列第n项",fib(9));
输出:34
const fibSuper = (item) =>{
if(item<2){
return item;
}
const numArr = [0,1];
for(let i =0; i<=item; i++){
numArr.push(numArr[0]+numArr[1]);
numArr.splice(0,1);
}
return numArr[1];
}
console.log("6.动态规划方法求斐波那契数列第N项",fibSuper(8));
输出:55
const getMaxProfit = (arr)=>{
let maxProfit = 0;
let minVal = arr[0];
for(var i =0; i
输出:9
const arrSort = (arr)=>{
for(let i = 0; i
输出:[ 1, 2, 3, 4, 5, 10 ]
const arrIntersection = (arr1,arr2)=>{
const result = arr1.filter(item1=>arr2.some(item2=>item1===item2));
return result;
}
console.log("9.数组交集",arrIntersection([1,2,3],[2,3,4,5,6]));
输出:[ 2, 3 ]
const arrComplement = (arr1,arr2)=>{
const result = arr2.filter(item2=>arr1.every(item1=>item1!==item2));
return result;
}
console.log("10.数组补集",arrComplement([1,2,3],[2,3,4,5,6]));
输出:[ 4, 5, 6 ]
const arrMerge = (arr1,arr2)=>{
const result = arr1.concat(arr2.filter(item2=>arr1.every(item1=>item1!==item2)));
return result;
}
console.log("11.数组并集",arrMerge([1,2,3],[2,3,4,5,6]));
输出:[ 1, 2, 3, 4, 5, 6 ]
const data= [{key:‘name’,value:‘jeck’},{key:‘age’,value:20}]
实现一个函数processFn根据对应的key,value生成对象
const res = {name:“jeck”,age:20}
const processFn = (data)=>{
const result = {};
for(let i =0; i
输出:{ name: ‘jeck’, age: 20 }
const arrSum = (arr)=>{
const temp = arr.reduce((pre,now)=>{
return pre+now;
},0);
return temp;
}
console.log("13.数组求和",arrSum([1,2,5]));
输出:8
输出例如:{A:2}
const arrRepeatMap = (arr)=>{
let obj = {};
arr.map((temp,i)=>{
let item = arr[i];
obj[item] = (obj[item]+1) || 1;
});
return obj;
}
console.log("14.1计算数组中重复的元素以及个数",arrRepeatMap(["A","A","B","C","C","D"]));
输出:{ A: 2, B: 1, C: 2, D: 1 }
或者
const arrRepeatFor = (arr)=>{
let obj = {};
for(let i = 0; i
输出:{ A: 2, B: 1, C: 2, D: 1 }
看完文章,感谢各位支持一下,不胜感激!
时间和机会,是留给有准备的人。
后面会继续记录前端面试题,持续更新;