I have not done Javascript programming in almost a decade. I see that it has changed a lot. Below is code that displays colourful block digits. Any constructive feedback is welcome.
function getRandomColor() {
const colours = ["#FF6663", "#94FF63", "#63FFEA", "#A763FF", "#F2FF63"];
return colours[Math.floor(Math.random() * colours.length)];
}
class Square extends React.Component {
render() {
var squareStyle = {
width: 25,
height: 25,
backgroundColor: this.props.colour || getRandomColor(),
display: "inline-block"
};
return (
<div style={squareStyle}>
</div>
);
}
}
class Digit extends React.Component {
render() {
const divStyle = {
display: "inline-block",
lineHeight: 0,
paddingLeft: "10px",
paddingRight: "10px"
};
const filled = [
[
[1, 1, 1],
[1, 0, 1],
[1, 0, 1],
[1, 0, 1],
[1, 1, 1]
],
[
[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 0, 1]
],
[
[1, 1, 1],
[0, 0, 1],
[1, 1, 1],
[1, 0, 0],
[1, 1, 1]
],
[
[1, 1, 1],
[0, 0, 1],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]
],
[
[1, 0, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1],
[0, 0, 1]
],
[
[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]
],
[
[1, 1, 1],
[1, 0, 0],
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
],
[
[1, 1, 1],
[0, 0, 1],
[0, 0, 1],
[0, 0, 1],
[0, 0, 1]
],
[
[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
],
[
[1, 1, 1],
[1, 0, 1],
[1, 1, 1],
[0, 0, 1],
[1, 1, 1]
]
];
let rows = [];
for (let y = 0; y < 5; y++) {
for (let x = 0; x < 3; x++) {
let objKey = y * 5 + x;
if (filled[this.props.digit][y][x]) {
rows.push(
<Square key={objKey} />
);
}
else {
rows.push(
<Square colour="#FFFFFF" key={objKey} />
);
}
}
rows.push(
<br key={y + "-br"} />
);
}
return (
<div style={divStyle}>
{rows}
</div>
);
}
}
ReactDOM.render(
<div>
<Digit digit={0} />
<Digit digit={5} />
<Digit digit={9} />
</div>,
document.getElementById("root")
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
To use, add <Digit digit={n}/> (where 0 <= n <= 9). In the future, I plan to change class Digit to class Character (which supports 0-9, A-Z and some punctuation characters). I will change the implementation to something like:
filled = {
"A": [
[0, 1, 0],
[1, 0, 1],
[1, 1, 1],
[1, 0, 1],
[1, 0, 1]
]
}
For those curious, this is what the digits 0 to 9 look like:
(the 1 is right-aligned like the 1 on a digital clock face)
