Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Traditional Switch Case / Else if
/**
* This method will validate user's status
* @param user user data
*/
function validateStatus(user) {
let response = {};
/*
* User status check: return response as per user status
*/
switch (user.status) {
//////////////////////////////
// Active //
//////////////////////////////
case "Active":
response = { message: "Login successful", user: user };
break;
//////////////////////////////
// Inactive //
//////////////////////////////
case "Inactive":
response = { message: "User is inactive", user: undefined };
break;
//////////////////////////////
// Invited //
//////////////////////////////
case "Invited":
response = { message: "User is not activated yet", user: undefined };
break;
//////////////////////////////
// Blocked //
//////////////////////////////
case "Blocked":
response = { message: "User is blocked", user: undefined };
break;
//////////////////////////////
// Invalid Status //
//////////////////////////////
default:
response = { message: "User with invalid status", user: undefined };
break;
}
/*
* Return response
*/
return response;
}
/**
* This method will validate user's role
* @param user user data
*/
function validateRole(user) {
let response = {};
if (user.role === "Admin") {
response = { message: "Full Access granted", user: user };
} else if (user.role === "Teacher") {
response = { message: "Limited Access granted", user: user };
} else if (user.role === "Student") {
response = { message: "Access denied", user: undefined };
}
/*
* Return response
*/
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment