Read the upload validation code. Find the bug — it uses == instead of === and compares against the wrong MIME type:
// Bug: file.type is "image/jpeg" (string)
// comparing incorrectly:
if (file.type == "image/jpg") {
// Wrong! MIME type is "image/jpeg", not "image/jpg"
const validTypes = ["image/jpeg", "image/png", "image/webp"];
if (validTypes.includes(file.type)) {
// proceed with upload
} else {
console.error("Invalid file type:", file.type);
}
Using an array + .includes() is better than chaining || comparisons — easier to read and extend.
const maxSizeMB = 25;
const maxSizeBytes = maxSizeMB * 1024 * 1024;
if (file.size > maxSizeBytes) {
console.warn(
`File too large: ${(file.size / 1024 / 1024).toFixed(1)}MB (max ${maxSizeMB}MB)`
);
}
What if no file was selected? What if the file has zero size?
Think about every way this could fail — then handle each case.
if (!file) {
console.error("No file selected");
} else if (!validTypes.includes(file.type)) {
console.error(
`Invalid type: ${file.type}. Accepted: JPEG, PNG, WebP`
);
} else if (file.size > maxSizeBytes) {
console.error(
`File too large: ${(file.size/1024/1024).toFixed(1)}MB. Max: ${maxSizeMB}MB`
);
} else {
console.log("File valid! Proceeding...");
}
Boolean algebra and truth tables.
Every if statement evaluates a boolean expression. && (AND) is true only if BOTH sides are true. || (OR) is true if EITHER side is true.