0

Here's my code.

function measureRequiredFruit(fruitTotals) {
  if (fruitTotals[bananas] > 3 && fruitTotals[strawberries] > 10){// if there are more than 3 bananas and more than 10 strawberries
    return 'We have enough fruit, with " + totalBananas + " bananas, and " + totalStrawberries + " strawberries';// return 'We have enough fruit, with {totalBananas} bananas, and {totalStrawberries} strawberries'
  }
  else{// otherwise
    return 'We do not have enough of both fruits';// return 'We do not have enough of both fruits'
  }
}

var result1 = measureRequiredFruit({bananas: 4, strawberries: 12});
console.log('should log "We have enough fruit, with 4 bananas, and 12 strawberries":', result1);

I'm trying to use the bananas key, which should be 4, in my function measureRequiredFruit. But I get an error message saying that 'bananas is undefined'. Why is this?

StLouis9
  • 15
  • 7
  • 1
    because it is looking for a variable `bananas`. `fruitTotals["bananas"]` or `fruitTotals.bananas` – epascarello Jul 28 '20 at 00:16
  • That worked -- could you elaborate on why please? Isn't bananas indeed a variable? – StLouis9 Jul 28 '20 at 00:26
  • because it is looking for `var bananas ` and you have no `bananas` defined. The variable there has no relationship to the property names in your object. – epascarello Jul 28 '20 at 00:31
  • But why isn't bananas a variable of the object fruitTotals? – StLouis9 Jul 28 '20 at 00:33
  • because it is not a variable it is a property of an object. You access a property with dot or bracket notation. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – epascarello Jul 28 '20 at 00:35

0 Answers0