JS代码重构求助:如何在switch中嵌入函数精简计算器代码
改进基础运算计算器代码的方案
当然可以通过整合运算逻辑和配置信息来精简switch语句里的重复代码,以下是两种适合初学者的改进思路:
方案一:用对象映射替代switch(最精简易维护)
将所有操作的运算函数、提示文本、结果描述统一存入一个配置对象,直接通过操作编号获取对应配置,避免重复编写输入输出逻辑:
// 精简版基础运算计算器 const operations = { '1': { func: (a, b) => a + b, aPrompt: 'Enter the value for A: ', bPrompt: 'Enter the value for B: ', resultText: 'The addition result is' }, '2': { func: (a, b) => a - b, aPrompt: 'Enter the value for A: ', bPrompt: 'Enter the value for B: ', resultText: 'The subtraction result is' }, '3': { func: (a, b) => a * b, aPrompt: 'Enter the value for A: ', bPrompt: 'Enter the value for B: ', resultText: 'The multiplication result is' }, '4': { func: (a, b) => a / b, aPrompt: 'Enter the value for A (dividend): ', bPrompt: 'Enter the value for B (divisor): ', resultText: 'The division result is' } }; console.log('Choose the number for the operation you want to use.'); console.log('1 - Addition'); console.log('2 - Subtraction'); console.log('3 - Multiplication'); console.log('4 - Division'); const calcAB = prompt('Operation: '); // 校验操作编号是否合法 if (operations[calcAB]) { const { func, aPrompt, bPrompt, resultText } = operations[calcAB]; const a = Number(prompt(aPrompt)); const b = Number(prompt(bPrompt)); console.log(`${resultText} "${func(a, b)}"`); } else { console.log('Invalid operation number!'); }
优点
- 所有操作的配置集中管理,后续新增运算只需在
operations对象里添加新键值对,无需修改核心逻辑 - 彻底消除了重复的输入输出代码,代码结构更清晰
方案二:保留switch但精简重复逻辑
如果你想继续使用switch语句,可以把重复的输入输出逻辑抽离,switch仅负责赋值对应的运算函数和文本:
//Calculator of Basic Operations // 用箭头函数简化运算函数定义 const addition = (a, b) => a + b; const subtraction = (a, b) => a - b; const multiplication = (a, b) => a * b; const division = (a, b) => a / b; console.log('Choose the number for the operation you want to use.'); console.log('1 - Addition'); console.log('2 - Subtraction'); console.log('3 - Multiplication'); console.log('4 - Division'); let calcAB = prompt('Operation: '); // 声明变量存储当前操作的配置 let operationFunc, aPrompt, bPrompt, resultText; switch (calcAB) { case '1': operationFunc = addition; aPrompt = 'Enter the value for A: '; bPrompt = 'Enter the value for B: '; resultText = 'The addition result is'; break; case '2': operationFunc = subtraction; aPrompt = 'Enter the value for A: '; bPrompt = 'Enter the value for B: '; resultText = 'The subtraction result is'; break; case '3': operationFunc = multiplication; aPrompt = 'Enter the value for A: '; bPrompt = 'Enter the value for B: '; resultText = 'The multiplication result is'; break; case '4': operationFunc = division; aPrompt = 'Enter the value for A (dividend): '; bPrompt = 'Enter the value for B (divisor): '; resultText = 'The division result is'; break; default: console.log('Invalid operation number!'); // 浏览器环境下用return终止后续执行,Node环境用process.exit() return; } // 统一执行输入和输出逻辑 const a = Number(prompt(aPrompt)); const b = Number(prompt(bPrompt)); console.log(`${resultText} "${operationFunc(a, b)}"`);
优点
- 保留了你原本熟悉的switch结构,更容易理解和过渡
- 消除了每个case里重复的
prompt和console.log代码
内容的提问来源于stack exchange,提问作者Duca Barros ヅカ 두카 牡羊埴
相关产品推荐
相关产品推荐

