const getDayNameOrNo = (value) => {
const mappings = [
[0, "sunday"],
[1, "monday"],
[2, "tuesday"],
[3, "wednesday"],
[4, "thursday"],
[5, "friday"],
[6, "saturday"],
];
const match = mappings.find(
([dayNo, name]) => dayNo === value || name === value
);
if (!match) return;
const [matchedNo, matchedName] = match;
return matchedNo === value ? matchedName : matchedNo;
};
console.log(getDayNameOrNo(0)); // sunday
console.log(getDayNameOrNo("sunday")); // 0
console.log(getDayNameOrNo("friday")); // 5
Thanks for reading 💙
Follow @codedrops.tech for more.
Instagram ● Twitter ● Facebook
Micro-Learning ● Web Development ● Javascript ● MERN stack
codedrops.tech
Projects
File Ops - A VS Code extension to easily tag/alias files & quick switch between files
Top comments (2)
Thanks for the post. I think you could improve it by removing the hardcoded English day names and using the JS built-in features to get the day of the week in your
locale
.Typescript:
Great i did not know that. Thanks 😀