Asked by amaya battle
Write a function favoriteColors that takes one parameter: colors (an array of three strings). The function should return a string in the format: “My favorite colors are [color1], [color2], and [color3].”
Answer-
function favoriteColors(colors) {
const [c1, c2, c3] = colors;
return `My favorite colors are ${c1}, ${c2}, and ${c3}.`;
}
Fix this code, make it correct, make sure the colors are clarified not just (c1,c2,c3)
All Answers 1
Answered by
GPT-5 mini
AI
Here's a corrected version with clearer variable names and input validation:
function favoriteColors(colors) {
if (!Array.isArray(colors) || colors.length !== 3) {
throw new TypeError('favoriteColors expects an array of exactly 3 color strings.');
}
const [color1, color2, color3] = colors;
if ([color1, color2, color3].some(c => typeof c !== 'string')) {
throw new TypeError('All three items in the array must be strings.');
}
return `My favorite colors are ${color1}, ${color2}, and ${color3}.`;
}
Example:
favoriteColors(['red', 'green', 'blue']);
// "My favorite colors are red, green, and blue."
function favoriteColors(colors) {
if (!Array.isArray(colors) || colors.length !== 3) {
throw new TypeError('favoriteColors expects an array of exactly 3 color strings.');
}
const [color1, color2, color3] = colors;
if ([color1, color2, color3].some(c => typeof c !== 'string')) {
throw new TypeError('All three items in the array must be strings.');
}
return `My favorite colors are ${color1}, ${color2}, and ${color3}.`;
}
Example:
favoriteColors(['red', 'green', 'blue']);
// "My favorite colors are red, green, and blue."
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.