I am just a beginner in Typescript and I am going over a practice assessment question about Enums in Typescript. I am totally confused with this question:
Extend the Permission enum so that it has the following fields:
Read, which should have a value of 1
Write, which should have a value of 2
Execute, which should have a value of 4
Extend the getPermissionList function so that it accepts a combination of several Permission enums and returns an array of strings which are the names of the Permissions.
For example, executing:
getPermissionList(Permission.Read | Permission.Write); should return [ 'Read', 'Write' ] in any order.
So far I have something like this but I don't know if am in the ballpark:
function getPermissionList(permission: Permission): string[] {
let arr: string[]
for (let enumMember in permission) {
arr.push(enumMember);
}
return arr;
}
enum Permission {
Read = 1,
Write = 2,
Execute = 4,
}
//console.log(getPermissionList(Permission.Read | Permission.Write));
I know it isn't right as I am getting errors but I feel like im banging my head against a wall right now.
Permission.Read | Permission.Writeis not two parameters. It's a binary OR of two enum values, as in the linked question. Please check that one and compare with your case; the only major difference I can see is that enum keys are not extracted there.Permissionis just an object that looks like{Read: 1, Write: 2, Execute: 4, "1": "Read", "2": "Write", "4": "Execute"}. So you will need to do bitwise arithmetic onpermissionand then use the set bits to pull keys or values out of thePermissionobject. You could maybe do it this way but there's lots of ways to approach it.