1. 写一个函数,传入两个较长的字符串(字符串元素为数字),返回相加后的字符串
输入 '1236', '999'
输出 '2235'
思路:
- 将字符串转为数组并反转数组
- 从左往右相等下标的元素相加,若大于等于十则进一
function test(a,b){
let arr1 = [...a].reverse().map(item=> parseInt(item))
let arr2 = [...b].reverse().map(item=> parseInt(item))
let maxArr = arr1.length >= arr2.length ? arr1 : arr2
let arr = maxArr.map((item,index) => {
arr1[index] = arr1[index] ? arr1[index] : 0
arr2[index] = arr2[index] ? arr2[index] : 0
if (arr1[index] + arr2[index] >= 10) {
// 取余数并返回
item = (arr1[index] + arr2[index]) % 10
maxArr[index+1] = (maxArr[index+1] ? maxArr[index+1] : 0) + 1
} else {
item = arr1[index] + arr2[index]
}
return item
})
return arr.reverse().join('')
}
2. 传入两个数组,数组元素都是数字,返回两个数组相同的值组成的数组,按照正序排序
输入 [4, 2, 1, 2, 5, 10], [2, 3, 1, 6, 10]
输出 [1, 2, 10]
思路:
- 先去重,再遍历对比找出重复的值
- 最后正序排序(sort排序规则需要自己手写)
const arrayA = [4, 2, 1, 2, 5];
const arrayB = [2, 3, 1, 6];
function process(arrayA, arrayB) {
let newArrA = [...new Set([...arrayA])]
let newArrB = [...new Set([...arrayB])]
let arr = newArrA.filter(item => {
return newArrB.includes(item)
})
return arr.sort((a,b) => a-b)
}
3. 如果有n个台阶,每次走一步或者走两步,一共有多少种走法(斐波那契额数列)
function test (n) {
if(n === 1) return 1
if(n === 2) return 2
let num1 = 1
let num2 = 2
let num = num1 + num2
for (let i=3; i<n; i++) {
num1 = num2
num2 = num
num = num1 + num2
}
return num
}
4. 找出数组中出现最多的元素和次数
5. 实现一个函数,对传入的数字每3位加一个逗号
function test (num) {
let arr1 = [], arr2 = [],arr = [] // arr1保存小数前, arr2保存小数后
arr = num.toString().split('.')
arr2 = arr[1] ? [...arr[1]] : [] // 判断是否存在小数,并将每项转为数组元素
arr1 = [...arr[0]]
let newArr1 = arr1.map((item, index) => arr1.length === (index+1) && (index+1)%3 === 0 ? item : (index+1)%3 === 0 ? item+',' : item)
let newArr2 = arr2.map((item, index) => arr2.length === (index+1) && (index+1)%3 === 0 ? item : (index+1)%3 === 0 ? item+',' : item) // 数组为空则map()不检测
newArr2.unshift('.')
console.log(newArr1.concat(newArr2).join(''))
}
test(123456789.123)
5. 实现jsonp
function jsonp(url, callback) {
// 定义回调函数
window['callback'] = function() {}
let script = document.createElement('script')
script.src = url + '?fn=' + callback
document.body.appendChild(script)
}
6. 给定一个数字数组,找到数组每个值后面第一个大于的它自身的元素,如果没找到,设为-1。最后返回一个包含所有找到的值的新数组
输入:[1, 5, 8, 7, 2, 9, 2]
输出:[5, 8, 9, 9, 9, -1, -1]
7. N个维度的数组,合并成一个一维数组 (数组的扁平化)
输入:[1, 2, [3, 4], [5, 6, [7, 8]]]
输出:[1, 2, 3, 4, 5, 6, 7, 8]
- Array.prototype.flat(n) // n为递归的深度
flat() 函数以递回方式将特定深度的子阵列重新串接成为一新的阵列
- forEach()
let arr = [1, 2, [3, 4], [5, 6, [7, 8]]];
function test(arr) {
let newArr = [];
arr.forEach((item) => {
newArr = newArr.concat(Array.isArray(item) ? test(item) : item);
});
return newArr;
}
console.log(test(arr));
- reduce()
function reduce(arr) { return arr.reduce((item1, item2) => { return item1.concat(Array.isArray(item2) ? reduce(item2) : item2); }, []); }
8. n*n二维数组,每行自左向右逐渐自增,每列自上向下自增,给定值,找出坐标
思路:
- 倒序遍历第一行,若值比目标值大,说明该列的最小值比目标值大,则删除值所在的列
- 所值比目标值小,则该行最大值比目标值要小,则删除值所在的行
- 依次重复1,2步骤,直到找到目标值
9. 写一个函数,传入数组和N,N为任意数;输出数组各项相加之和为N组成的数组,不可重复
输入 arr = [1, 2,3,4,5,0,-1, 2, -3, 0, 0, -1] ,N = 0
输出 [[1, -1], [3, -3], [0, 0]]
10. 传入接口地址组成的数组,最大并行请求数,执行接口请求,任意请求完成后填补上
思路:
- 根据传入的最大并行数创建相应长度的数组并填充null
- 遍历新建数组,并取接口数组首项传入处理函数
- 处理函数执行接口请求,在then回调中再去取首项接口并递归
const urls = ['a','b','c','d'] // 接口数组
const m = 2 // 最大并行数
// 入口函数,新建数组及遍历
function scheduleFetch(urls, m) {
// 创建长度为m的数组
let arr = new Array(m).fill(null);
arr.forEach(item => {
// 获取urls的首项并删除
let url = urls.shift()
handler(urls, url)
})
}
// 处理函数,传入剩余接口数组和地址
function handler(urls, url) {
if (!url) return
fetch(url).then((res) => {
let url = urls.shift()
handler(urls, url) // 递归执行
})
}
// 执行函数,返回promise
const fetch = (url) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(url)
}, Math.random() * 1000)
})
}