Build a Recipe Ingredient Converter - Step 9

Tell us what’s happening:

Not sure what I am doing wrong if someone can help… thanks! :slight_smile:

Your code so far

/* file: script.js */
const conversionTable = {
  cup: { gram: 240, ounce: 8.0, teaspoon: 48 },
  gram: { cup: 1 / 240, ounce: 0.0353, teaspoon: 0.2 },
  ounce: { cup: 0.125, gram: 28.35, teaspoon: 6 },
  teaspoon: { cup: 1 / 48, gram: 5, ounce: 0.167 },
}


// User Editable Region

const convertQuantity = (fromUnit) => (toUnit) => (quantity) => {
 
  return conversionTable.fromUnit.toUnit * quantity
}
console.log(convertQuantity("gram")("cup")(1))

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0

Challenge Information:

Build a Recipe Ingredient Converter - Step 9

https://www.freecodecamp.org/learn/full-stack-developer/workshop-recipe-ingredient-converter/step-9

const convertQuantity = (fromUnit) => (toUnit) => (quantity) => {

return conversionTable[fromUnit][toUnit]* quantity
}

// Instead of dot notation Bracket notation needs to be used.

Reason : because fromUnit and toUnit are variables, not fixed property names.

Dot notation (obj.key) looks for a property literally named “key”.

Bracket notation (obj[key]) evaluates the variable key and uses its value to look up the property.

1 Like

I didn’t know that. thanks!

1 Like