Enumerations

This page is under construction. Please come back later.
#!/usr/bin/env node;
const Utils = require('./Utils');
const CompassPoint = Object.freeze({
NORTH: 0,
EAST: 1,
SOUTH: 2,
WEST: 3
});
function compassPointToString(c) {
let result = "";
switch (c) {
case CompassPoint.NORTH:
result = "North";
break;
case CompassPoint.EAST:
result = "East";
break;
case CompassPoint.SOUTH:
result = "South";
break;
case CompassPoint.WEST:
result = "West";
break;
}
return result;
}
function turnRight(c) {
if (c === CompassPoint.NORTH) {
return CompassPoint.EAST;
} else if (c === CompassPoint.EAST) {
return CompassPoint.SOUTH;
} else if (c === CompassPoint.SOUTH) {
return CompassPoint.WEST;
} else {
return CompassPoint.NORTH;
}
}
const main = async () => {
let cp1 = CompassPoint.NORTH;
console.log(["cp1: ", Utils.intToEnum(CompassPoint, cp1)].join(''));
console.log(["SOUTH: ", compassPointToString(CompassPoint.SOUTH)].join(''));
console.log(["turnRight(cp1): ", compassPointToString(turnRight(cp1))].join(''));
const CP2 = CompassPoint.EAST;
if (cp1 === CP2) {
console.log("cp1 == cp2");
} else {
console.log("cp1 != cp2");
}
cp1 = CP2;
if (cp1 === CP2) {
console.log("cp1 == cp2");
} else {
console.log("cp1 != cp2");
}
}
main().catch( e => { console.error(e) } );
Output
$ node Enums1.js
cp1: 0
SOUTH: South
turnRight(cp1): East
cp1 != cp2
cp1 == cp2
#!/usr/bin/env node;
const Utils = require('./Utils');
const Color = Object.freeze({
RED: 1,
GREEN: 2,
YELLOW: 3,
BLUE: 4,
MAGENTA: 5,
CYAN: 6
});
function combineColors(c1, c2) {
const i1 = c1;
const i2 = c2;
const RESULT = i1 | i2;
const RETURN_COLOR = RESULT;
return RETURN_COLOR;
}
const main = async () => {
let c1 = Color.BLUE;
console.log(["c1: ", Utils.intToEnum(Color, c1)].join(''));
console.log(["RED: ", Utils.intToEnum(Color, Color.RED)].join(''));
console.log(["combineColors(c1, RED): ", Utils.intToEnum(Color, combineColors(c1, Color.RED))].join(''));
const c2 = Color.GREEN;
console.log(["combineColors(c1, c2): ", Utils.intToEnum(Color, combineColors(c1, c2))].join(''));
if (c1 === c2) {
console.log("c1 == c2");
} else {
console.log("c1 != c2");
}
c1 = c2;
if (c1 === c2) {
console.log("c1 == c2");
} else {
console.log("c1 != c2");
}
const BAD = 7;
console.log(["bad: ", Utils.intToEnum(Color, BAD)].join(''));
}
main().catch( e => { console.error(e) } );
Output
$ node Enums2.js
c1: 4
RED: 1
combineColors(c1, RED): MAGENTA
combineColors(c1, c2): CYAN
c1 != c2
c1 == c2
bad: null
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]
Pure Programmer


