[英]Simplified javascript some function with almost the same code
Hei, I'm working an app to simulate prices. 嘿,我正在开发一个应用程序来模拟价格。 I have a code like this. 我有这样的代码。
function max110(x) {
if (x >= '1' && x <= '50') {
var sum = 120 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else if (x >= '51' && x <= '100') {
var sum = 115 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else if (x >= '101' && x <= '200') {
var sum = 110 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else {
hasil.value = 'error!';
}
}
function max115(x) {
if (x >= '1' && x <= '50') {
var sum = 125 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else if (x >= '51' && x <= '100') {
var sum = 120 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else if (x >= '101' && x <= '200') {
var sum = 115 * x;
hasil.value = 'Rp.' + parseFloat(sum * 1000);
} else {
hasil.value = 'error!';
}
}
And I still have some functions similar to that, it almost the same code I'm trying to make it simple, is it possible to make it in 1 function only? 而且我还有一些类似的功能,它几乎与我试图简化的代码相同,是否可以仅在1个功能中使用它?
Try: 尝试:
function maxval(x,maxval) {
if(x >= '1' && x <= '50'){
var sum = (maxval+10)* x;
hasil.value = 'Rp.'+parseFloat(sum*1000);
}
else if (x >= '51' && x <= '100'){
var sum = (maxval+5)* x;
hasil.value = 'Rp.'+parseFloat(sum*1000);
}
else if(x >= '101' && x <= '200'){
var sum = (maxval)* x;
hasil.value = 'Rp.'+parseFloat(sum*1000);
}
else{
hasil.value = 'error!';
}
}
By the way i assumed that maxval increments by 5, Cant get you a better solution without getting more details about functionality. 顺便说一句,我假设maxval会增加5,Cant为您提供了更好的解决方案,而没有更多有关功能的细节。
This would be my implementation. 这就是我的实现。 I don't agree with how you're handling your integers, but it's your coding style. 我不同意您如何处理整数,但这是您的编码风格。 I pass in an object of choice that has all of the values that I want. 我传入一个选择的对象,该对象具有我想要的所有值。 You don't need the logic, just the values. 您不需要逻辑,仅需要值。 I hope nobody gets mad that I monkeypatch String
. 我希望没有人生气的时候,我猴补丁String
。 I'm assuming that your variable x
is a string. 我假设变量x
是一个字符串。
String.prototype.isBetween = function(lower, upper){
int = parseInt(this)
return int >= parseInt(lower) && int <= parseInt(upper)
}
max110 = {0: 120, 1: 115, 2: 110}
max115 = {0: 125, 1: 120, 2: 115}
function max(x, values) {
let sum
hasil.value = ''
if (x.isBetween('1', '50')) {
sum = values['0'] * x
} else if (x.isBetween('51', '100')) {
sum = values['1'] * x
} else if (x.isBetween('101', '200')) {
sum = values['2'] * x
} else {
hasil.value = 'error'
}
hasil.value = hasil.value ? 'error' : 'Rp.'+parseFloat(sum*1000);
}
function max(x, extra) {
var sum_number = extra;
if(x >= '1' && x <= '50'){
sum_number += 120;
}
else if (x >= '51' && x <= '100'){
sum_number += 115;
}
else if(x >= '101' && x <= '200'){
sum_number += 110;
}
if(x < 1 && x > 200){
hasil.value = 'error!';
} else {
hasil.value = 'Rp.'+parseFloat((sum_number) * x *1000);
}
}
parameter extra
can be 0 or 5 for function max110
or max115
参数max110
或max115
参数extra
可以为0或5
Basically, you have two function which works the same way and returns the same with different values. 基本上,您有两个函数以相同的方式工作,并以不同的值返回相同的函数。
The different values yould be stored in an array and you could use a single function for getting the index and then take the needed value out of the array with that index. 您将不同的值存储在数组中,可以使用单个函数获取索引,然后使用该索引从数组中取出所需的值。
So you need a better organisation of types of the variables, which if uses as number, it should be number and also for comparison, then it should be a number on both sides of the condition. 因此,您需要更好地组织变量的类型,如果将其用作数字,则它应该是数字,并且也要进行比较,然后在条件的两边都应该是数字。
Use a pure function , which does not alter a state of something, which is not given into the function. 使用纯函数 ,该函数不会更改某些东西的状态,而该函数未赋予该函数。
Use a check in the function for unwanted values and exit early with a first check at the lower border, in your case, it is zero and below, return -1
, because that is not an index of an array (and it is usually used to denote, that no index is found, like with Array#indexOf
). 在函数中使用不需要的值的检查,并在下边界进行第一次检查以尽早退出,如果是零以下,则返回-1
,因为它不是数组的索引(通常使用表示没有找到索引,例如Array#indexOf
)。
Then take the upper border for a check end exit early with a index value, no need for continuing else if
structures. 然后使用索引值尽早获取检查结束出口的上限,而无需继续else if
结构。
At the end return as well -1
for not found index. 最后返回-1
,表示找不到索引。
Together: 一起:
function getValue(x, maxArray) { var index = getIndex(x); if (index in maxArray) { return 'Rp.' + maxArray[index] * x * 1000; } return 'error!'; } function getIndex(x) { if (!x || x < 0) { return -1; } if (x <= 50) { return 0; } if (x <= 100) { return 1; } if (x <= 200) { return 2; } return -1; } var max110 = [120, 115, 110], max115 = [125, 120, 115]; console.log(getValue(-1, max110)); console.log(getValue(10, max110)); console.log(getValue(10, max115));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.