Tell us what’s happening:
The drum machine is working but all the buttons play the same sound.
see project on codepen: https://codepen.io/Logan_code/full/poNwBMX
Your code so far
const arr = [
{
keyCode: 81,
key:"Q" ,
sound:'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'
},
{
keyCode: 87,
key:"W" ,
sound:'https://s3.amazonaws.co/freecodecamp/drums/Heater-2.mp3'
},
{
keyCode: 69,
key:"E" ,
sound:'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3'
},
{
keyCode: 65,
key:"A" ,
sound:'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3'
},
{
keyCode: 83,
key: "S",
sound:'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3'
},
{
keyCode: 68,
key: "D",
sound:'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3'
},
{
keyCode: 90,
key:"Z" ,
sound:'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3'
},
{
keyCode: 88,
key: "X",
sound:'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3'
},
{
keyCode: 67,
key: "C",
sound:'https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3'
}
]
class App extends React.Component{
constructor(props){
super(props);
this.audio = React.createRef();
this.playSound= this.playSound.bind(this);
}
playSound(){
this.audio.current.play();
}
render(){
return (
<div id="display">
{arr.map((arr,i) =>(
<div className="drum-pad" text={arr.key} key={i} id={arr.key} onClick={this.playSound}>{arr.key}
<audio className="clip" src={arr.sound} id={arr.key} ref={this.audio}/>
</div>
))}
</div>
);
}
};
ReactDOM.render(<App />, document.getElementById("drum-machine"));
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0
.
Challenge: Build a Drum Machine
Link to the challenge: