简体   繁体   English

代码在不同环境下表现不同

[英]Code performing differently on different enviornments

I've been studying for an upcoming exam and came across something odd.我一直在为即将到来的考试而学习,但遇到了一些奇怪的事情。 Here is the prompt for the question:这是问题的提示:

You have a fashion catalog, an inventory of items from various high-fashion designers.你有一个时尚目录,一个来自各种高级时装设计师的物品清单。 Each designer has a lineup of shoes.每个设计师都有一系列的鞋子。 Each shoe has a name and a price.每双鞋都有名字和价格。

It looks like this:它看起来像这样:

 var currentInventory = [
  {
    name: 'Brunello Cucinelli',
    shoes: [
      {name: 'tasselled black low-top lace-up', price: 1000},
      {name: 'tasselled green low-top lace-up', price: 1100},
      {name: 'plain beige suede moccasin', price: 950},
      {name: 'plain olive suede moccasin', price: 1050}
    ]
  },
  {
    name: 'Gucci',
    shoes: [
      {name: 'red leather laced sneakers', price: 800},
      {name: 'black leather laced sneakers', price: 900}
    ]
  }
]; 

Your function should return the average cost of all shoes per designer in this format:您的函数应该以这种格式返回每位设计师所有鞋子的平均成本:

var expected = {
  'designers': [
    {
      'name': 'Brunello Cucinelli',
      'averagePrice': 1025
    },
    {
      'name': 'Gucci',
      'averagePrice': 850
    }
  ]
};

I've written some code and it behaves differently whether I run it on Repl.it or the hack reactor practice exam website.我已经编写了一些代码,无论是在 Repl.it 还是在 hack reactor 练习考试网站上运行它,它的行为都不同。 Here is my code:这是我的代码:

function calculateAveragePricePerDesigner(inventory) {
  var result = {designers: []};
  var parsedDesignerAverageCost = [];
  for (i = 0; i < inventory.length; i++) {
    parsedDesignerAverageCost.push(parseAverageCostPerDesigner(inventory[i]));
    console.log(parsedDesignerAverageCost);
  }
  result.designers = parsedDesignerAverageCost;
  return result;
}


function parseAverageCostPerDesigner(currentDesigner) {
  var currentShoes = currentDesigner.shoes;
  var result = {
    name: currentDesigner.name,
    averagePrice: 0
  };
  var cummulativeCost = 0;
  var numOfShoes = currentShoes.length;
  for (i = 0; i < numOfShoes; i++) {
    cummulativeCost += currentDesigner.shoes[i].price;

  }
  var average = cummulativeCost / numOfShoes;
  result.averagePrice = average;
  return result;
}

Is anyone able to explain whether or not my code is correct and possibly why the inconsistency is occurring?有没有人能够解释我的代码是否正确以及可能发生不一致的原因? On Repl.it my code will compile but not run in a manner that I cannot explain, and on the hack reactor website it will not compile.在 Repl.it 上,我的代码将编译但不会以我无法解释的方式运行,并且在 hack reactor 网站上它不会编译。 T Thank you谢谢

You have a slight "mistake" here:你在这里有一个轻微的“错误”:

 for (i = 0; i < numOfShoes; i++) {

You never declare i .你从不声明i That turns i into an implicit global variable, thus both functions work with the same i .这将i变成了一个隐式全局变量,因此两个函数都使用相同的i That means that the inner function will increase i, and then the outer will continue with that.这意味着内部函数将增加 i,然后外部函数将继续增加 i。 Thus it skips some records.因此它跳过了一些记录。 The easy fix would be:简单的解决方法是:

 for (var i = 0; i < numOfShoes; i++) {

And this probably has to do with the sample data used to test your code.这可能与用于测试代码的示例数据有关。


Here's how I'd write the whole thing:这是我写整件事的方式:

 function averagePrice(shoes) {
   let sum = 0;
   for(const shoe of shoes)
      sum += shoe.price;
   return sum / shoes.length;
 }

 function task(designers) {
   let result = [];
   for(const designer of designers) {
     result.push({ name: designer.name, averagePrice: averagePrice(designer.shoes) });
    }
    return result;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 javascript 事件处理程序在不同的浏览器中执行不同? - Why javascript event handler is performing differently in different browser? 相同的代码,但在不同的视图中表现不同 - Same code but behaving differently in different view 相同代码的来源不同 - Identical code behaving differently from different sources 事件代码在不同浏览器中的行为不同 - Event Code Behaves Differently in Different Browsers 相同的代码在不同的设备上运行不同 - Same code runs differently on different devices jQuery .on()的执行与预期不同 - jQuery .on() performing differently than expected 相同的JavaScript代码在两种不同的环境中的行为不同 - Same JavaScript code behaves differently in two different environments 在不复制代码的情况下针对不同的Dojo版本以不同的方式声明类? - Declaring class differently for different Dojo versions without duplicating code? 尽管代码,版本和配置相同,但JShint在不同机器上的行为不同 - JShint acts differently on different machines despite same code, versions and config 几乎相同的代码,不同的输出。 什么是JavaScript在这里做的不同? - Almost same code, different output. What is JavaScript doing differently here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM