能帮忙看看这个函数怎么写吗?

2018-06-26 10:20:22 +08:00
 Martin9

一批货放在仓库里,计费方式如下:
1-7 天: 免费;
8-14 天:16/天;
15-21 天:32/天;
22-28 天:48/天;
以此类推。

首先要求是前端代码
传入的参数是天数。
例如放 15 天,费用就是 16x7+32=144

1356 次点击
所在节点    前端开发
5 条回复
rabbbit
2018-06-26 10:41:52 +08:00
let price = function(day) {
if (!day && !typeof(a) === 'number') return TypeError;
let amount = 0;
let i = 0;
while (day > 0) {
day -= 7;
let stepPrice = i * 16;
amount += day > 0 ? stepPrice * 7 : stepPrice * (7 + day);
i++;
}
return amount;
}

console.log(price(0));
console.log(price(1));
console.log(price(7));
console.log(price(8));
console.log(price(14));
console.log(price(15));
rabbbit
2018-06-26 10:45:42 +08:00
Martin9
2018-06-26 10:56:16 +08:00
@rabbbit 感谢,铜币已发
rabbbit
2018-06-26 11:16:47 +08:00
if (!day && !typeof(a) === 'number') return TypeError; -> if (typeof(day) !== 'number') return TypeError;
Martin9
2018-06-26 11:25:28 +08:00
@rabbbit 天数在 input 框里已经限制成 number 了。再次感谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/465878

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX