2.0/10
verdict: needs_serious_help
"Nested if-else catastrophe with no regard for scalability"
lang: JavaScript·34 lines
//your_submission
CaffeineDrivenDisaster.js
function calculatePrice(item: any, size: any) {
if (item == "coffee") {
if (size == "small") {
return "Price is $2";
} else if (size == "medium") {
return "Price is $3";
} else if (size == "large") {
return "Price is $4";
}
} else if (item == "tea") {
if (size == "small") {
return "Price is $1";
} else if (size == "medium") {
return "Price is $2";
} else if (size == "large") {
return "Price is $3";
}
} else if (item == "cake") {
if (size == "small") {
return "Price is $5";
} else if (size == "medium") {
return "Price is $7";
} else if (size == "large") {
return "Price is $10";
}
} else {
return "We dont have that";
}
}
const myItem = "coffee";
const mySize = "large";
const result = calculatePrice(myItem, mySize);
console.log(result);//detailed_analysis
!
Inefficient Conditional Statements
The code uses a ridiculous amount of nested if-else statements, making it impossible to maintain or extend. It's like trying to build a skyscraper with playing cards.
!
Lack of Data Structure
The prices are hardcoded in a series of if-else statements, instead of using a proper data structure like an object or array. This is a sign of a developer who doesn't understand the basics of programming.
?
No Input Validation
The function doesn't check if the input item or size is valid, it just assumes it will always be correct. This is a recipe for disaster, like handing a loaded gun to a toddler.
//suggested_fix
CaffeineDrivenDisaster_fixed.js
+const prices = {
+ coffee: { small: 2, medium: 3, large: 4 },
+ tea: { small: 1, medium: 2, large: 3 },
+ cake: { small: 5, medium: 7, large: 10 }
+};
+
+function calculatePrice(item, size) {
+ return prices[item] && prices[item][size] ? `Price is $${prices[item][size]}` : 'We dont have that';
+}
+
+const myItem = 'coffee';
+const mySize = 'large';
+const result = calculatePrice(myItem, mySize);
+console.log(result);