javascript - 函数在输出上复制数组
问题描述
我创建了一个包含 2 个函数的脚本,第一个计算数组的百分比,而另一个函数计算我的百分比与我的数组的总和。但是当我调用我的函数时,我的数组百分比的输出会加倍
const Info_Buyer = {
Name_Buyer: 'John',
Spent_Rest: [50, 100, 300, 10, 80],
Tip: [],
Total_Spent: 0,
// The function stores the amount spent for each restaurant and tips depending on the amount spent
Cal_Tip: function() {
let n = 0
while (n < this.Spent_Rest.length) {
if (this.Spent_Rest[n] < 50) {
this.Tip.unshift(this.Spent_Rest[n] * 0.2);
} else if (this.Spent_Rest[n] > 50 && this.Spent_Rest[n] < 200) {
this.Tip.unshift(this.Spent_Rest[n] * .15);
} else {
this.Tip.unshift(this.Spent_Rest[n] * .1);
}
n++
}
return this.Tip
},
// The function sums the value of the tip and the amount spent on the order, showing the total expense as output
Cal_Total: function() {
let tip = this.Cal_Tip()
let n = 0
while (n < this.Spent_Rest.length) {
this.Total_Spent += this.Spent_Rest[n] + tip[n]
n++
}
return this.Total_Spent
}
}
total = Info_Buyer.Cal_Tip()
tip = Info_Buyer.Cal_Total()
console.log(total, tip);
预期结果
解决方案
也许是这样的?
const Info_Buyer =
{ Name_Buyer : 'John'
, Spent_Rest : [50, 100, 300, 10, 80]
, Cal_Tip()
{ // The function stores the amount spent for each restaurant and tips depending on the amount spent
return this.Spent_Rest.map(x=>
{
if (x <= 50) return x * 0.2
if (x <= 200) return x * 0.15
return x * 0.1
})
}
, Cal_Total()
{ // The function sums the value of the tip and the amount spent on the order, showing the total expense as output
return this.Cal_Tip().reduce((ac,el,n)=>ac +el +this.Spent_Rest[n], 0)
}
}
let tip = Info_Buyer.Cal_Tip()
, total = Info_Buyer.Cal_Total()
console.log('tip =', JSON.stringify(tip));
console.log('total =', total );
.as-console-wrapper { max-height: 100% !important; top: 0; }
Expand snippet
在您的代码中,您执行以下操作:
function Cal_Tip() {
let val
if (x < 50) val = x * 0.2 // x = 0...49
else if(x>50 && < 200) val = x * 0.15 // x = 51...199
else val = x * 0.1 // x = 50 or x = 200...infinite
return val
}
你错过了x = 50!
else if(x>50 && < 200)
应该 是else if(x>=50 && < 200)
但它是无用的,因为0...49 的值之前已经过测试,并且在 else if 中不会发生
所以你的代码应该是:
function Cal_Tip() {
let val
if (x <= 50) val = x * 0.2
else if(x <= 200) val = x * 0.15
else val = x * 0.1
return val
}
我的代码是
function Cal_Tip() {
if (x <= 50 ) return = x * 0.2 // if (x < 50 ) the function is finished and return x * 0.2
if (x <= 200) return = x * 0.15 // if (x < 200) the function is finished and return x * 0.15
return = x * 0.1 // in aother cases...
}
javascript 在数组对象上提供了许多方法(在 JS 上,一切都是对象!)
方法 array.map 上 mdn 上的文档(带有示例)=> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
mdn 上关于方法 array.reduce => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
的文档
不要忘记为此使用 initialValue。
箭头函数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
快速示例:
function functionA(param) { return param *10 }
// become
functionA = param => param*10 // the arrow make the return too
function functionB(param1,param2) {
let sum = param1+param2;
return sum/2
}
// become
functionB =(param1,param2)=>{ // more than 1 arguments -> you should use parenthesis
let sum = param1+param2; // + more than 1 line of -> you should use brackets
return sum/2
}
推荐阅读
- c# - 有没有一种方法可以在检查变量是否为空的同时为变量赋值?
- flutter - 更改父部件高度而不导致子部件重建 Flutter
- python - Python:如果多行包含 abcd1234 ,我可以找到 abcd* 并替换吗?
- vue.js - Electron + Vue托盘错误的图标路径
- json - 如何在 json 列表中设置 grpc 响应
- apache-kafka - 带有 NestJS 的 Kafka - 发送挂起
- stata - 将虚拟变量聚合为多个分类变量
- javascript - 反应原生的滑块切换按钮?
- jmeter - jmeter结果树中缺少的项目
- python - 为什么我的 Python 代码认为一个变量应该是一个 int 却是一个 str?