我试着根据积分系统来计算某人的肠道健康状况。我可以以任何方式编辑数据结构或逻辑。我只是想写一个函数和数据结构来处理这种能力。
伪计算器函数:
// Bowel health calculator
var points = 0;
If age is from 30 and 34:
points += 1
If age is from 35 and 40:
points += 2
If daily BMs is from 1 and 3:
points -= 1
If daily BMs is from 4 and 6:
points -= 2
return points;伪数据结构:
var points_map = {
age:
{
'35-40': 1,
'40-45': 2,
'45-50': 6,
'50-55': 2,
'55-60': 1,
'60-65': 4,
'65-70': 3,
'70-75': 1,
'75-150': 2
},
dbm:
{
'1-3': -1,
'4-6': -2,
'7-9': -3,
'10-150': 5
}
// This plus about 10 more metrics like them (all with the same "Map a plus or minus value to each range" logic)
};我有一个完整的电子表格,像这样的数据,我正在尝试编写一个干式版本的代码和干式版本的数据(即,可能不是字符串的'30-34',等等),以便处理这种事情,而不是大量的switch语句。
发布于 2010-05-18 02:34:49
我认为一个更好的结构应该是这样的:
{
[35,1],
[40,2],
[45,6],
...
[150, null]
},然后对它执行一次循环:
for (var i =0; i<points_map.age.length-1; i++)
{
if (val >= points_map.age[i][0] && val<points_map.age[i+1][0])
points += points_map.age[i][1];
}如果需要的话,这可能会变得更有效率,但你已经明白了。
https://stackoverflow.com/questions/2851516
复制相似问题