I'm making a Karuta web game. In karuta, there are "kimariji", which are the minimum amount of "letters" needed to know which card to take. These counts will be stored in the API/JSON I've created, which get saved as a state and read from.
I'm looking to do this: (english example)
var kimarijiCount = 6
string = 'Elephant'
in this case, only 'Elepha' turns red.
var kimarijiCount = 2
string = 'Rhino'
in this case, only 'Rh' turns red.
How can I do this using javascript/react? I'm assuming my pseudocode would be:
//get string
//get count variable x
//set css for the first x chars to a different colour
This is how my code for creating the cards current looks, if it helps
render() {
if (this.props.displayType === "kanji") {
return (
<div id="CardContainer" className="cardContainer">
{this.props.cards.slice(0, this.props.number).map((cards, index) => (
<div id="card" className="cardOuter" onClick={() => this.props.handleComp(index)}>
<div className="cardInner">
<p key={index}>{cards.back}</p>
</div>
</div>
))}
</div>)
}
{cards.back.slice(0,3)}
{cards.back.slice(3, cards.back.length)}
`, but it only works if I change the page after the DOM has already been loaded. If I refresh the page, I just get a 'TypeError: Cannot read property 'slice' of undefined ' error message. – esvnt Dec 01 '20 at 07:32