Hi all,
I’m trying to create a new array of prepared foods, but having trouble. How do I use an array as a parameter for my mealMaker function? Am I invoking the cook and slice functions in the wrong place?
// Define a cook function
const cookMeat = (meat) => {
return "Cooked ${meat}";
};
// Define a slice function
const sliceVeg = (vegetable) => {
return "${vegetable} slices";
};
// Define a mealMaker function
function mealMaker() {
var preparedFood = [];
for (let i=0; i < array.length; i++) {
if (array[i].type=="meat") {
let meat = array[i].type;
let preparedMeat = cookMeat();
preparedFood.push(preparedMeat);
}
else if (array[i].type==="vegetable") {
let vegetable = array[i].type;
let preparedVeg = sliceVeg();
preparedFood.push(preparedVeg);
}
}
}
const arrayOfFoodObjects = [
{"food": "beef",
"type": "meat"
},
{"food": "zucchini",
"type": "vegetable"
},
{"food": "beet",
"type": "vegetable"
},
{"food": "chicken",
"type": "meat"
}
];
// Invoke the mealMaker function and pass the array to it as an argument
mealMaker(arrayOfFoodObjects);
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
Note: Backticks are not single quotes.
so I made some progress by getting rid of the double quotes in my return statements
// Define a cook function
const cookMeat = (meat) => {
return `Cooked ${meat}`;
};
// Define a slice function
const sliceVeg = (vegetable) => {
return `${vegetable} slices`;
};
// Define a mealMaker function
function mealMaker(array) {
var preparedFood = [];
for (let i=0; i < array.length; i++) {
if (array[i].type==="meat") {
let meat = array[i].type.value;
let preparedMeat = cookMeat(meat);
preparedFood.push(preparedMeat);
}
else if (array[i].type==="vegetable") {
let vegetable = array[i].type.value;
let preparedVeg = sliceVeg(vegetable);
preparedFood.push(preparedVeg);
}
}
return preparedFood;
}
// Define four objects in the following array. Two of each type
const arrayOfFoodObjects = [
{"food": "beef",
"type": "meat"
},
{"food": "zucchini",
"type": "vegetable"
},
{"food": "beet",
"type": "vegetable"
},
{"food": "chicken",
"type": "meat"
}
];
// Invoke the mealMaker function and pass the array to it as an argumentconsol
mealMaker(arrayOfFoodObjects);
but it still results in the following array
[ 'Cooked undefined',
'undefined slices',
'undefined slices',
'Cooked undefined' ]
not sure why part of it is undefined