【问题标题】:Refactoring JavaScript (Swtich & Function)重构 JavaScript (Swtich & Function)
【发布时间】:2022-11-01 12:28:16
【问题描述】:
我是编码的初学者,我正在学习JS。我想知道如何在下面的代码中的开关内编写一个函数(如果可能的话),将代码变小。
我试图将每个操作的功能放在开关内,但它从来没有奏效。
帮助我改进我的代码。谢谢!
//Calculator of Basic Operations
function addition(a, b) {
return (a + b);
}
function subtraction(a, b) {
return (a - b);
}
function multiplication(a, b) {
return (a * b);
}
function division(a, b) {
return (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: ');
switch (calcAB) {
case '1':
a = Number(prompt('Enter the value for A: '));
b = Number(prompt('Enter the value for B: '));
console.log(`The addition result is "${addition(a, b)}"`);
break;
case '2':
a = Number(prompt('Enter the value for A: '));
b = Number(prompt('Enter the value for B: '));
console.log(`The subtraction result is "${subtraction(a, b)}"`);
break;
case '3':
a = Number(prompt('Enter the value for A: '));
b = Number(prompt('Enter the value for B: '));
console.log(`The multiplication result is "${multiplication(a, b)}"`);
break;
case '4':
a = Number(prompt('Enter the value for A (dividend): '));
b = Number(prompt('Enter the value for B (divisor): '));
console.log(`The division result is "${division(a, b)}"`);
break;
}
【问题讨论】:
标签:
javascript
node.js
refactoring
【解决方案1】:
案件之间唯一改变的是
- 调用的函数
- 调用的操作的名称
-
(dividend)(divisor)/
我会为函数和运算符名称使用一个数组——例如,[0] 将引用(a, b) => a + b,因此您只需从用户选择的数字中减去 1 即可访问该函数。要获得a 和b,仅当case 为4 时插入(dividend) (divisor) - 但它可以一次完成。
const fns = [
[(a, b) => a + b, 'addition'],
[(a, b) => a - b, 'subtraction'],
[(a, b) => a * b, 'multiplication'],
[(a, b) => a / b, 'division']
];
const op = prompt('Operation: ');
const item = fns[op - 1];
if (!item) {
throw new Error('Invalid');
}
const a = Number(prompt(`Enter the value for A${op === '4' ? ' (dividend)' : ''}: `));
const b = Number(prompt(`Enter the value for b${op === '4' ? ' (divisor)' : ''}: `));
console.log(`The ${item[1]} result is ${item[0](a, b)}`);
【解决方案2】:
我想你的意思是这样的,对吧?
//Calculator of Basic Operations
function addition(a, b) {
return (a + b);
}
function subtraction(a, b) {
return (a - b);
}
function multiplication(a, b) {
return (a * b);
}
function division(a, b) {
return (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: ');
switch (calcAB) {
case '1':
console.log(`The addition result is "${addition(...enter())}"`);
break;
case '2':
console.log(`The subtraction result is "${subtraction(...enter())}"`);
break;
case '3':
console.log(`The multiplication result is "${multiplication(...enter())}"`);
break;
case '4':
console.log(`The division result is "${...enter())}"`);
break;
}
function enter() {
a = Number(prompt('Enter the value for A: '));
b = Number(prompt('Enter the value for B: '));
return [a, b];
}