This is my code
const amountOfGuests = 6;
const poundsOfBeefShanks = 0.17;
const poundOfBeefTendons = 0.17;
const cupsOfWater = 1.33;
const largeOnion = 0.17;
const teaspoonOfPeppercorns = 0.17;
const tablespoonsOfFishSauce = 0.33;
const corn = 0.5;
const mediumPotatoes = 0.33;
const smallCabbage = 0.17;
const greenBeans = 2;
const valueOfBeefShanks = amountOfGuests * poundsOfBeefShanks;
const valueOfBeefTendons = amountOfGuests * poundOfBeefTendons;
const valueOfWater = amountOfGuests * cupsOfWater;
const valueOfOnion = amountOfGuests * largeOnion;
const valueOfPeppercorns = amountOfGuests * teaspoonOfPeppercorns;
const valueOfFishSauce = amountOfGuests * tablespoonsOfFishSauce;
const valueOfCorn = amountOfGuests * corn;
const valueOfPotatoes = amountOfGuests * mediumPotatoes;
const valueOfCabbage = amountOfGuests * smallCabbage;
const valueOfGreenBeans = amountOfGuests * greenBeans;
"In order to cook my dish Nilagang Baka for " amountOfGuests
" guests I will need " valueOfBeefShanks ' pound beef shanks, '
valueOfBeefTendons " pound beef tendons, "
valueOfWater " cups of water, " valueOfOnion " large onion, "
valueOfPeppercorns " teaspoon of peppercorns, "
valueOfFishSauce " tablespoon of fish sauce, "
valueOfCorn " corn, " valueOfPotatoes " medium potatoes, "
valueOfCabbage " small cabbage, and " valueOfGreenBeans " green beans. "
How can I insert at least one function and array in it? Free to change the code. I was also to tasked to add a recipe with it.
CodePudding user response:
Your code is not maintainable and if the ingredients grow then it would be a nightmare to maintain and understand.
You can create a new function getInstruction
that will just return the value
required.
Use Object.keys and reduce to loop over object properties
function getInstruction(amountOfGuests, ingredients) {
return `In order to cook my dish Nilagang Baka for "${amountOfGuests} guests I will need "${ingredients.poundsOfBeefShanks} pound beef shanks, "${ingredients.poundOfBeefTendons} pound beef tendons, "${ingredients.cupsOfWater} cups of water, "${ingredients.largeOnion} large onion, "${ingredients.teaspoonOfPeppercorns} teaspoon of peppercorns, "${ingredients.tablespoonsOfFishSauce} tablespoon of fish sauce, "${ingredients.corn} corn, "${ingredients.mediumPotatoes} medium potatoes, "${ingredients.smallCabbage} small cabbage, and "${ingredients.greenBeans} green beans.`;
}
const amountOfGuests = 6;
const ingredients = {
poundsOfBeefShanks: 0.17,
poundOfBeefTendons: 0.17,
cupsOfWater: 1.33,
largeOnion: 0.17,
teaspoonOfPeppercorns: 0.17,
tablespoonsOfFishSauce: 0.33,
corn: 0.5,
mediumPotatoes: 0.33,
smallCabbage: 0.17,
greenBeans: 2,
};
const ingredientsValueObj = Object.keys(ingredients).reduce((acc, curr) => {
acc[curr] = amountOfGuests * ingredients[curr];
return acc;
}, {});
const result = getInstruction(amountOfGuests, ingredientsValueObj);
console.log(result);
CodePudding user response:
You can create an object containing the variable keys and their value in pairs. You can then traverse the keys of that object to perform a continuous printout, very similar to what you have concatenated. For example: You can change the static variables to an array of variables, and then do a for loop to loop through and concatenate them.
var recipt = {
poundsOfBeefShanks : 0.17,
poundOfBeefTendons : 0.17,
cupsOfWater : 1.33,
largeOnion : 0.17,
teaspoonOfPeppercorns : 0.17,
tablespoonsOfFishSauce : 0.33,
corn : 0.5,
mediumPotatoes : 0.33,
smallCabbage : 0.17,
greenBeans : 2
}
This would be your object. Then iterate it by a for, but first capture the keys
var keys = Object.keys(recipt);
var text = ""
keys.forEach(element => {
text = element ": " recipt[element] ", "
});
console.log(text)
// The result is
poundsOfBeefShanks: 0.17, poundOfBeefTendons: 0.17, cupsOfWater: 1.33, largeOnion: 0.17, teaspoonOfPeppercorns: 0.17, tablespoonsOfFishSauce: 0.33, corn: 0.5, mediumPotatoes: 0.33, smallCabbage: 0.17, greenBeans: 2,