字节2面
// implement a function to calculate a formula string with 'plus' and 'minus'
// no need to worry about big number problem
console.log(calc(test)) // 11
var test = '-1+31-1-1-1-24+100-92' // 13
function rev(num) { return ~~Array.from(num).reverse().join('') }
function calc(str) {
let cur = ''
let arr = []
for(let i = str.length - 1 ; i >= 0; i --) {
let c = str[i];
if(!isNaN(Number(c))) {
cur += c
} else {
arr.push(c === '+' ? rev(cur) : -1 * rev(cur))
cur = ''
}
}
return arr.reduce((a, b) => a + b)
}
console.log(calc(test))
八股
async function async1() {
console.log('async1 start');
await async2();
console.log('async1 end');
}
async function async2() {
console.log('async2');
}
console.log('script start');
setTimeout(function() {
console.log('setTimeout');
}, 0);
async1();
new Promise(function(resolve) {
console.log('promise1');
resolve();
}).then(function() {
console.log('promise2');
});
console.log('script end');
// answer
// 'script start'
// 'async1 start'
// 'async2'
// 'promise1'
// 'script end'
// 'async1 end'
// 'prpmise2'
// 'setTimeout''script start'
// 'async1 start'
// 'async2'
// 'promise1'
// 'script end'
// 'async1 end'
// 'prpmise2'
// 'setTimeout'
0507 字节三面 微信红包的随机算法 给金额和人数,返回红包金额