diff --git "a/code/interview/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.js" "b/code/interview/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.js" index ef90f4605d..46beb90ffa 100644 --- "a/code/interview/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.js" +++ "b/code/interview/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.js" @@ -1,15 +1,22 @@ const addStrings = (num1, num2) => { - let res = ""; - let i = num1.length - 1; - let j = num2.length - 1; - let flag = 0; - while (i >= 0 || j >= 0 || flag !== 0) { - if (i >= 0) flag += parseInt(num1[i--]); - if (j >= 0) flag += parseInt(num2[j--]); - // 取余 - res = (flag % 10) + res; - flag = Math.floor(flag / 10); + let res = '' + let i = num1.length - 1 + let j = num2.length - 1 + let flag = 0 + while(i >= 0 || j >= 0) { + let sum = flag; + if (i >= 0) sum += parseInt(num1[i--]) + if (j >= 0) sum += parseInt(num2[j--]) + + flag = Math.floor(sum / 10) + + res = sum % 10 + res; } + + if (flag > 0) { + res = flag + res; + } + return res; }; diff --git "a/code/interview/\345\256\236\347\216\260\345\271\266\345\217\221\346\216\247\345\210\266.js" "b/code/interview/\345\256\236\347\216\260\345\271\266\345\217\221\346\216\247\345\210\266.js" new file mode 100644 index 0000000000..e7961a0c40 --- /dev/null +++ "b/code/interview/\345\256\236\347\216\260\345\271\266\345\217\221\346\216\247\345\210\266.js" @@ -0,0 +1,85 @@ +async function asyncPool(limit, fns) { + const res = [] + const exec = [] + + for (const item of fns) { + const p = Promise.resolve().then(() => item()) + + res.push(p) + + if (limit <= fns.length) { + const execute = p.then(() => { + exec.splice(exec.indexOf(execute), 1) + }) + exec.push(p) + console.log(exec.length) + if (exec.length >= limit) { + console.log('达到并发数量', limit) + await Promise.race(exec) + } + } + } + + return Promise.all(res) +} + +// 示例测试用例 +async function runTest() { + const taskLimit = 3; // 最大并发数 + const tasks = [ + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 1'); + return 1; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 2'); + return 2; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 3'); + return 3; + }, + async () => { + await sleep(500); // 模拟异步任务 + console.log('执行完成 4'); + return 4; + }, + async () => { + await sleep(2000); // 模拟异步任务 + console.log('执行完成 5'); + return 5; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 6'); + return 6; + }, + async () => { + await sleep(3000); // 模拟异步任务 + console.log('执行完成 7'); + return 7; + }, + async () => { + await sleep(500); // 模拟异步任务 + console.log('执行完成 8'); + return 8; + }, + ]; + + console.log(`Running asyncPool with a limit of ${taskLimit}`); + const results = await asyncPool(taskLimit, tasks); + console.log(`Results: ${results}`); +} + +// 辅助函数,模拟异步任务 +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +// 运行测试用例 +runTest().then(() => { + console.log('执行完毕'); +}).catch(console.error); \ No newline at end of file diff --git "a/src/frontend-basic/js/0.1+0.2\344\270\272\344\273\200\344\271\210\344\270\215\347\255\211\344\272\2160.3.md" "b/src/frontend-basic/js/0.1+0.2\344\270\272\344\273\200\344\271\210\344\270\215\347\255\211\344\272\2160.3.md" index 94404c92e1..2f1e6cf68c 100644 --- "a/src/frontend-basic/js/0.1+0.2\344\270\272\344\273\200\344\271\210\344\270\215\347\255\211\344\272\2160.3.md" +++ "b/src/frontend-basic/js/0.1+0.2\344\270\272\344\273\200\344\271\210\344\270\215\347\255\211\344\272\2160.3.md" @@ -4,4 +4,12 @@ ECMAScript 中的 Number 类型使用 IEEE754 标准来表示整数和浮点数 ECMAScript 采用的就是双精确度,也就是说,会用 `64位` 来储存一个浮点数。 -0.1的二进制是无限循环的,0.2的二进制也是无限循环的。所以0.3是不准确的 \ No newline at end of file +0.1的二进制是无限循环的,0.2的二进制也是无限循环的。所以0.3是不准确的 + + +## 如何求小数二进制 +0.625 * 2 = 1.25,所以第一位是1。 +小数部分变为0.25。 +0.25 * 2 = 0.5,所以第二位是0。 +小数部分变为0.5。 +0.5 * 2 = 1,所以第三位是1 \ No newline at end of file diff --git "a/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260Promise\345\271\266\345\217\221\346\216\247\345\210\266.md" "b/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260Promise\345\271\266\345\217\221\346\216\247\345\210\266.md" index cebfcac90d..076d8277c2 100644 --- "a/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260Promise\345\271\266\345\217\221\346\216\247\345\210\266.md" +++ "b/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260Promise\345\271\266\345\217\221\346\216\247\345\210\266.md" @@ -2,45 +2,93 @@ ## 方式一 ```js -async function asyncPool(poolLimit, array, iteratorFn) { - const ret = []; //2 - const executing = []; //3 - for (const item of array) { //4 - const p = Promise.resolve().then(() => iteratorFn(item)); //5 - ret.push(p); //6 - if (poolLimit <= array.length) { //7 - const e = p.then(() => executing.splice(executing.indexOf(e), 1)); //8 - executing.push(e); //9 - if (executing.length >= poolLimit) { //10 - await Promise.race(executing); //11 +async function asyncPool(limit, fns) { + const res = [] + const exec = [] + + for (const item of fns) { + const p = Promise.resolve().then(() => item()) + + res.push(p) + + if (limit <= fns.length) { + const execute = p.then(() => { + exec.splice(exec.indexOf(execute), 1) + }) + exec.push(p) + if (exec.length >= limit) { + await Promise.race(exec) } } } - return Promise.all(ret); //15 + + return Promise.all(res) } ``` ```js -const curl = (i) => { - console.log('开始' + i); - return new Promise((resolve) => setTimeout(() => { - resolve(i); - console.log('结束' + i); - }, 1000+Math.random()*1000)); -}; - -/* -const curl = (i) => { - console.log('开始' + i); - return i; -}; -*/ -let urls = Array(10).fill(0).map((v,i) => i); -(async () => { - const res = await asyncPool(3, urls, curl); - console.log(res); - })(); + +// 示例测试用例 +async function runTest() { + const taskLimit = 3; // 最大并发数 + const tasks = [ + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 1'); + return 1; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 2'); + return 2; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 3'); + return 3; + }, + async () => { + await sleep(500); // 模拟异步任务 + console.log('执行完成 4'); + return 4; + }, + async () => { + await sleep(2000); // 模拟异步任务 + console.log('执行完成 5'); + return 5; + }, + async () => { + await sleep(1000); // 模拟异步任务 + console.log('执行完成 6'); + return 6; + }, + async () => { + await sleep(3000); // 模拟异步任务 + console.log('执行完成 7'); + return 7; + }, + async () => { + await sleep(500); // 模拟异步任务 + console.log('执行完成 8'); + return 8; + }, + ]; + + console.log(`Running asyncPool with a limit of ${taskLimit}`); + const results = await asyncPool(taskLimit, tasks); + console.log(`Results: ${results}`); +} + +// 辅助函数,模拟异步任务 +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + +// 运行测试用例 +runTest().then(() => { + console.log('执行完毕'); +}).catch(console.error); ``` ## 方式二 diff --git "a/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.md" "b/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.md" index 2a746977e3..302bd8b66d 100644 --- "a/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.md" +++ "b/src/frontend-basic/js/\346\211\213\345\206\231\351\242\230/\345\256\236\347\216\260\345\244\247\346\225\260\347\233\270\345\212\240.md" @@ -1,18 +1,34 @@ # 实现大数相加 ```js -const addStrings = (num1, num2) => { - let res = ""; - let i = num1.length - 1; - let j = num2.length - 1; - let flag = 0; - while (i >= 0 || j >= 0 || flag !== 0) { - if (i >= 0) flag += parseInt(num1[i--]); - if (j >= 0) flag += parseInt(num2[j--]); - // 取余 - res = (flag % 10) + res; - flag = Math.floor(flag / 10); +const addStrings = (a, b) => { + let res = ''; + let i = a.length - 1; + let j = b.length - 1; + let flag = 0 + while(i >= 0 || j >= 0) { + let sum = 0; + if (i >= 0) sum += parseInt(a[i--]) + if (j >= 0) sum += parseInt(b[j--]) + // 加上上次的进位 + sum += flag; + + // 更新进位 + flag = Math.floor(sum / 10) + + // 计算值 + res = sum % 10 + res; + } + + // 说明第一位相加 还是有进位 + if (flag > 0) { + res = flag + res; } return res; -}; +} + +console.log(addStrings('1234', '2345')); // 输出 '3589' +console.log(addStrings('987654321098765432109876543210', '123456789012345678901234567890')); // 输出 '1111111110111111110111111111000' +console.log(addStrings('999999', '1')); // 输出 '1000000' +console.log(addStrings('0', '0')); // 输出 '0' ```