I am new to React and I am trying to figure out how I can shuffle an array. I have tried different way to shuffle the array but the problem is always with the length. TypeError: Cannot read property 'length' of undefined. I am not sure why this is happening or how I should deal with it in React. To me, this shouldn't be an issue with React but again I don't know. What should happen when everything works:
You click on the button 'hard' and it should render the two first elements from the array challenge, and every time you click on that button the array should be shuffled.
Do you have any idea that could help me?
This is the code:
import React from 'react';
import ReactDOM from 'react-dom';
let era = [
60, 70, 80, 90, 2000
]
let genre = [
'Rock', 'Pop', 'Jazz', 'Country'
]
let challenge = [
'LikeElvis', 'Parent\'s favourite songs', 'too high for me'
]
class Karaoke extends React.Component {
constructor(props){
super(props);
this.state = {
'easy': '',
'easyChallenge': '',
'hard': '',
'hardChallenge': '',
'hardEra': ''
}
};
selectEasy = () => {
let genreSelected = [];
let challengeSelected = [];
genreSelected.push(genre[Math.floor(Math.random() * genre.length)]);
this.setState({"easy": genreSelected})
challengeSelected.push(challenge[Math.floor(Math.random() * challenge.length)]);
this.setState({"easyChallenge": challengeSelected})
}
selectHard = () => {
let genreSelected = [];
let challengeSelected = [];
let eraSelected = [];
genreSelected.push(genre[Math.floor(Math.random() * genre.length)]);
eraSelected.push(era[Math.floor(Math.random() * era.length)]);
this.setState({ "hard": genreSelected} );
this.setState({ "hardEra": eraSelected} );
this.setState({ "hardChallenge": challengeSelected} );
challengeSelected.push(challenge.slice(0, 2))
console.log(challengeSelected);
}
shuffle = (challenge) => {
let i = challenge.length, temporaryValue, randomIndex;
while (0 !== i) {
randomIndex = Math.floor(Math.random() * i);
i -= 1;
temporaryValue = challenge[i];
challenge[i] = challenge[randomIndex];
challenge[randomIndex] = temporaryValue;
}
return challenge
}
click = () => {
this.selectHard();
this.shuffle();
}
render = () => {
return(
<div>
<button onClick={this.selectEasy}>Easy</button>
<button onClick={this.click}>Hard</button>
<h1>the genre is {this.state.easy} </h1>
<h1>the challenge is {this.state.easyChallenge} </h1>
<h1>Hard mode: {this.state.hard} + {this.state.hardEra + '\'s'}</h1>
<h1>Hard mode challenge: {this.state.hardChallenge} </h1>
</div>
)
}
}
ReactDOM.render(<Karaoke />, document.getElementById('root'));
shuffle = (challenge) => {removechallengefrom this line and should work properly, probably.