简体   繁体   English

为什么我的函数返回与Chrome浏览器中的console.log()不同

[英]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.logreturn是我期望的。 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.

相关问题 为什么我的function的结果和直接console.log的结果不一样? - Why is the result in my function and the result at direct console.log is different? 我的js数组不会在chrome浏览器中与console.log一起显示 - My js array wont display with console.log in chrome browser 当我在浏览器中输入 console.log 时,为什么谷歌浏览器没有显示结果? - When I typed console.log to my browser, why does Google Chrome not showing me the result? 为什么我的 JS 函数没有在 HTML 中打印(显示在 console.log 中) - Why is my JS function not printing in HTML (showing in console.log) 我的 console.log() 在我的 chrome 控制台中显示了两次 - my console.log() shows up twice in my chrome console Console.log在我的功能范围内不起作用 - Console.log not working within my function 如何返回或console.log函数返回选择的数组? - How do I return or console.log my function to return my array of choice? Chrome Javascript 控制台不输出 function console.log,为什么? - Chrome Javascript console is not outputting function console.log, why? 浏览器 console.log 和代码 console.log 显示不同 - Browser console.log and code console.log showing different 我的代码使用 console.log 离线运行,但不在浏览器中 - My code runs offline with console.log but not in browser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM