[英]Why is my function return different than my console.log() in Chrome browser
I am completely stumped. 我完全陷入了困境。 I don't even know if I am asking this question correctly.
我什至不知道我是否正确地问了这个问题。 Here is my repl>it http://repl.it/BAQ0/151 My
console.log
and my return
is what I expect here. 这是我的代表http://repl.it/BAQ0/151我的
console.log
和return
是我期望的。 But when I place the code into my code editor window for my training I get [array[2]]
from console.log
and my code will not pass. 但是,当我将代码放置到代码编辑器窗口中进行培训时,我从
console.log
获得了[array[2]]
,并且我的代码无法通过。 I have added the test that is not passing below the code in the repl>it. 我在repl> it中的代码下面添加了未通过的测试。 What am I missing?
我想念什么? Thanks!
谢谢!
var values = {
'ONE HUNDRED':10000,
'TWENTY':2000,
'TEN':1000,
'FIVE':500,
'ONE':100,
'QUARTER':25,
'DIME':10,
'NICKEL':5,
'PENNY':1
};
var change = [];
var array = [];
function multValuesBy100(arr) {
//return arr with larger values
for(var i = 0; i < arr.length; i++) {
arr[i][1] *= 100;
}
return arr;
}
function giveChange(changeDue,arr) {
var rem = changeDue;
for(var key in values) {
var n = Math.floor(rem/values[key]);
if(n !== 0) {
change.push([key, ((n*values[key])/100)]);
}
rem = changeDue % values[key];
}
console.log(change);
return change;
}
function changeInDrawer(cid) {
//returns a number representing the amount of change in the drawer
var result = 0;
for(var i = 0; i < cid.length; i++) {
result += cid[i][1]*100;
}
return result;
}
function drawer(price, cash, cid) {
price *= 100;
cash *= 100;
var changeDue = cash - price;
// Here is your change, ma'am.
if(changeInDrawer(cid) < changeDue) {
return "Insufficient Funds";
} else if(changeInDrawer(cid) === changeDue) {
return "Closed";
} else {
return giveChange(changeDue, cid);
}
}
// Example cash-in-drawer array:
// [['PENNY', 1.01],
// ['NICKEL', 2.05],
// ['DIME', 3.10],
// ['QUARTER', 4.25],
// ['ONE', 90.00],
// ['FIVE', 55.00],
// ['TEN', 20.00],
// ['TWENTY', 60.00],
// ['ONE HUNDRED', 100.00]]
drawer(19.50, 20.00, [['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.10], ['QUARTER', 4.25], ['ONE', 90.00], ['FIVE', 55.00], ['TEN', 20.00], ['TWENTY', 60.00], ['ONE HUNDRED', 100.00]]);
//------------------ Test below -----------------//
/*assert.deepEqual(drawer(19.50, 20.00, [['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.10], ['QUARTER', 4.25], ['ONE', 90.00], ['FIVE', 55.00], ['TEN', 20.00], ['TWENTY', 60.00], ['ONE HUNDRED', 100.00]]), [['QUARTER', 0.50]], 'return correct change');return correct change: expected [ Array(2) ] to deeply equal [ [ 'QUARTER', 0.5 ] ]*/
Probably because you define the change
out of giveChange
, so if the tester run something else before, there's probably when you run the current test, the change has already have some value which is pushed by previous tests. 可能是因为您是通过
giveChange
定义change
的,所以如果测试人员之前运行过其他操作,则可能是在运行当前测试时,该更改已经具有一些由先前测试推动的值。
Try declare that inside giveChange
, so it would be like : 尝试在
giveChange
内声明该giveChange
,如下所示:
function giveChange(changeDue,arr) {
// Ensure we use a pure array.
var change = [];
var rem = changeDuem;
for(var key in values) {
var n = Math.floor(rem/values[key]);
if(n !== 0) {
change.push([key, ((n*values[key])/100)]);
}
rem = changeDue % values[key];
}
console.log(change);
return change;
}
Another point is [array[2]]
is the unexpanded format when chrome
log object
or array
which contains non-primitive types. 另一点是
[array[2]]
是chrome
日志object
或包含非基本类型的array
时的未扩展格式。 Repl.it should just expaned it for user to read easier, the result should be the same. Repl.it应该只是扩展它以便用户阅读,结果应该是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.