Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

javascript - 为什么我的函数不返回值?(Why is my function not returning a value?)

My johnTipAvg function is console logging the average but it is not returning it for some reason.

(我的johnTipAvg函数是控制台记录平均值,但是由于某种原因它没有返回平均值。)

I have markTipAvg at the bottom which is returning the value just fine so I really do not understand why this is not working...

(我在底部的markTipAvg返回值就很好了,所以我真的不明白为什么这不起作用...)

I have checked for typos and could not find anything.

(我检查了错别字,找不到任何东西。)

Also, messed around with the position of the functions and I moved around the function calls.

(同样,弄乱了函数的位置,我在函数调用周围移动。)

Where am I going wrong with this?

(我在哪里错呢?)

 var john = { bills: [124, 48, 268, 180, 42], tips: [], finalBill: [], calcTip: function(bill) { for (var i = 0; i < bill.length; i++) { if (bill[i] < 50) { this.tips.push(bill[i] * .2); this.finalBill.push(bill[i] + (bill[i] * .2)); } else if (bill[i] > 50 && bill[i] < 200) { this.tips.push(bill[i] * .15); this.finalBill.push(bill[i] + (bill[i] * .15)); } else { this.tips.push(bill[i] * .10); this.finalBill.push(bill[i] + (bill[i] * .10)); } } } } function johnTipAvg(tip) { var sum = 0; for (var i = 0; i < tip.length; i++) { sum += tip[i]; } var avg = sum / (tip.length - 1); console.log(avg); return avg; } john.calcTip(john.bills); johnTipAvg(john.tips); var mark = { bills: [77, 375, 110, 45], tips: [] } function markCalcTips(bill) { var sum; for (var i = 0; i < bill.length; i++) { if (bill[i] < 100) { mark.tips.push(bill[i] * .2); } else if (bill[i] > 100 && bill[i] < 300) { mark.tips.push(bill[i] * .1); } else { mark.tips.push(bill[i] * .25); } } } function markTipAvg(tip) { var sum = 0; for (var i = 0; i < tip.length; i++) { sum += tip[i]; } var avg = sum / (tip.length - 1); return avg; } markCalcTips(mark.bills); markTipAvg(mark.tips); 

  ask by Dnyce translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

markTipAvg() is returning fine, as you can see in this snippet:

(您可以在此代码段中看到, markTipAvg()返回的状态很好:)

 var john = { bills: [124, 48, 268, 180, 42], tips: [], finalBill: [], calcTip: function(bill){ for(var i = 0; i < bill.length; i++){ if(bill[i] < 50){ this.tips.push(bill[i] * .2); this.finalBill.push(bill[i] + (bill[i] * .2)); } else if(bill[i] > 50 && bill[i] < 200){ this.tips.push(bill[i] * .15); this.finalBill.push(bill[i] + (bill[i] * .15)); } else { this.tips.push(bill[i] * .10); this.finalBill.push(bill[i] + (bill[i] * .10)); } } } } function johnTipAvg(tip){ var sum = 0; for(var i = 0; i < tip.length; i++){ sum += tip[i]; } var avg = sum / (tip.length -1); console.log(avg); return avg; } console.log("john.calcTip(john.bills) = " + john.calcTip(john.bills)); console.log("johnTipAvg(john.tips) = " + johnTipAvg(john.tips)); var mark = { bills: [77, 375, 110, 45], tips: [] } function markCalcTips(bill){ var sum; for(var i = 0; i < bill.length; i++){ if(bill[i] < 100){ mark.tips.push(bill[i] * .2); } else if(bill[i] > 100 && bill[i] < 300){ mark.tips.push(bill[i] * .1); } else { mark.tips.push(bill[i] * .25); } } } function markTipAvg(tip){ var sum = 0; for(var i = 0; i < tip.length; i++){ sum += tip[i]; } var avg = sum / (tip.length -1); return avg; } console.log("markCalcTips(mark.bills) = " + markCalcTips(mark.bills)); console.log("markTipAvg(mark.tips) = " + markTipAvg(mark.tips)); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...