記錄一些在筆試過程當中遇到的題目,作法不必定對,若是你對於題目有其餘想法,歡迎討論。前端
class Event {
constructor() {
this._cache = {}
}
on(type, cb) {
let fns = (this._cache[type] = this._cache[type] || [])
if (fns.indexOf(cb) === -1) {
fns.push(cb)
}
return this
}
trigger(type, data) {
let fns = this._cache[type]
if (Array.isArray(fns)) {
fns.forEach(fn => {
fn(data)
})
}
return this
}
off(type, cb) {
let fns = this._cache[type]
if (Array.isArray(fns)) {
if (cb) {
let index = fns.indexOf(cb)
if (index !== -1) {
fns.splice(index, 1)
}
} else {
fns.length = 0
}
}
return this
}
}
const event = new Event()
event.on('test', a => {
console.log(a)
})
event.trigger('test', 'water')
event.off('test')
event.trigger('test', 'hello world')
複製代碼
// 正則 零寬斷言
function exchange(num) {
num += ''
if (num.length <= 3) return num
num = num.replace(/\d{1,3}(?=(\d{3})+$)/g, function(v) {
return v + ','
})
return num
}
console.log(exchange(1234567))
//
const num = 2333333
num.toLocaleString()
複製代碼
/**
利用rand5()函數生成1-25之間的數字,而後將其中的1-21映射成1-7,丟棄22-25。
**/
function rand7() {
var res = 0
do {
res = (rand5() - 1) * 5 + rand5()
} while(res > 21)
return 1 + res % 7
}
複製代碼
function shuffle(arr) {
return arr.sort(function() {
return Math.random() - 0.5
})
}
複製代碼
function count(str) {
return [...new Set(str.split(''))]
.map(v => [v, str.match(new RegExp(v, 'g')).length])
.sort((a, b) => b[1] - a[1])
.slice(0, 1)
}
複製代碼
function subset(arr1, arr2) {
if ((!arr1 instanceof Array) || (!arr2 instanceof Array)) return false
if (arr2.length < arr1.length) return false
for (let i = 0, len = arr1.length; i < len; i++) {
if (arr2.indexOf(arr1[i]) === -1) return false
}
return true
}
複製代碼
const arr = [[1,2],3,[4,5,6]];
[[1,3,4],[2,3,4],[1,3,5],[2,3,5],[1,3,6],[2,3,6]]
複製代碼
function multiply(arr) {
let ret = []
function cur(result, index) {
if (index === -1) {
ret.push(result)
} else {
let items = Array.isArray(arr[index]) ? arr[index] : [arr[index]]
items.forEach(item => {
cur([item, ...result], index - 1)
})
}
}
cur([], arr.length - 1)
return ret
}
const arr = [
[1, 2], 3, [4, 5, 6]
];
console.log(multiply(arr))
複製代碼
function Router() {
this.routes = {}
this.currentUrl = ''
}
Router.prototype.route = function(path, callback) {
this.routes[path] = callback || function() {}
}
Router.prototype.refresh = function() {
this.currentUrl = location.hash.slice(1) || '/'
this.routes[this.currentUrl]()
}
Router.prototype.init = function() {
window.addEventListener('load', this.refresh.bind(this), false)
window.addEventListener('hashchange', this.refresh.bind(this), false)
}
window.Router = new Router()
window.Router.init()
Router.route('/', function() {
console.log('white')
})
Router.route('/blue', function() {
console.log('blue')
})
Router.route('/green', function() {
console.log('green')
})
複製代碼
var arr = [1, 4, 6, 9, 11, 15, 18]
function arrIndexOf(arr, val) {
var mid,
min = 0,
max = arr.length - 1
while (min <= max) {
mid = (min + max) >> 1
if (val > arr[mid]) {
min = mid + 1
} else if (val < arr[mid]) {
max = mid - 1
} else {
return mid
}
}
return min
}
function insert(arr, val) {
if (arr[0] === val) {
arr.unshift(val)
} else if (arr[arr.length - 1] === val) {
arr.push(val)
} else {
arr.splice(arrIndexOf(arr, val), 0, val)
}
return arr
}
// or
function insert(arr, val) {
arr.push(val)
return arr.sort((a, b) => {
return a - b
})
}
複製代碼
HardMan("jack")
// 輸出 'I am jack'
HardMan("jack").rest(10).learn("computer")
// 輸出 'I am jack'
// 10s後輸出
// 'Start learning after 10 second'
// 'Learning computer'
HardMan("jack").restFirst(5).learn("chinese")
// 5s後輸出
// 'Start learning after 5 second'
// 'I am jack'
// 'Learning chinese'
複製代碼
const HardMan = name => {
class HardMan {
constructor(name) {
this.queue = [this.init(name)]
setTimeout(async () => {
for (let todo of this.queue) {
await todo()
}
}, 0)
}
init(name) {
return () => console.log(`I am ${name}`)
}
learn(subject) {
this.queue.push(() => console.log(`Learning ${subject}`))
// 鏈式調用
return this
}
holdon(time) {
return () => new Promise(resolve => setTimeout(() => {
resolve(console.log(`Start learning after ${time} second`))
}, time * 1000))
}
rest(time) {
this.queue.push(this.holdon(time))
return this
}
restFirst(time) {
this.queue.unshift(this.holdon(time))
return this
}
}
return new HardMan(name)
}
複製代碼
場景1 團隊成員都頗有愛,都願意借錢給其餘同事,那麼這時候團隊最多能租到多少房子數組
function max(Array n, Array m, S) {
return num
}
複製代碼
場景2 團隊成員都十分小氣,是不肯意借錢給別人的bash
//請判斷團隊成員是否都能租到房子
function isAll(Array n, Array m, S){
return bool
}
複製代碼
作法待商榷微信
場景1:dom
function max(Array n, Array m, S) {
let count = 0
n.forEach(item => {
count += item
})
count += S
let num = 0
let mSort = m.slice().sort((a, b) => {
return a - b
})
mSort.forEach(item => {
count -= item
if (count >= 0) num++
})
return num
}
複製代碼
場景2:async
function isAll(n, m, S) {
let bool = null;
let arr = new Array(n.length);
const avg = S/n;
n.forEach((item, index) => {
arr[index] = item + avg
});
let mSort = m.slice().sort((a, b) => {
return a - b
});
let arrSort = arr.slice().sort((a, b) => {
return a - b
});
for(let i = 0, len = arrSort.length; i < len; i++) {
if (arrSort[i] < mSort[i]) {
bool = false;
break;
}
bool = true;
}
return bool;
}
複製代碼
function toCount(num) {
let arr = num.toString().split('')
return arr.reduce((pre, next, index, array) => {
if (next === '0') {
return pre
} else {
let item = parseInt(next) * Math.pow(10, array.length - index - 1)
pre.push(item)
return pre
}
}, [])
}
//or
function toCount1(num) {
let arr = num.toString().split('')
let len = arr.length
let ret = []
arr.forEach((item, index) => {
if (item != 0) {
ret.push(parseInt(item + '0'.repeat(len - index - 1)))
}
})
return ret
}
console.log(toCount1(60040))
複製代碼
Array.prototype.extraChar = function () {
var cacheExtraChar = []
var that = this
this.map(function (item) {
(that.indexOf(item) !== that.lastIndexOf(item)) &&
cacheExtraChar.indexOf(item) === -1 ? cacheExtraChar.push(item) : -1
})
return cacheExtraChar
}
複製代碼