Tell us what’s happening:
I have written the code but do not understand why I do not recieve an output. Here is a link to my code pen (https://codepen.io/RDL123/pen/zYQOVBM)
Your code so far
<!DOCTYPE html>
<html lang ="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, intitial-scale =1.0"/>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<script src="https://unpkg.com/react@16/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
-->
<div id="drummachine">
</div>
<script type="text/babel" src="./index.js"></script>
const audioClips = [
{
keyCode: 81,
keyTrigger: "Q",
id: "Heater-1",
url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"
}, {
keyCode: 87,
keyTrigger: "W",
id: "Heater-2",
url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"
}, {
keyCode: 69,
keyTrigger: "E",
id: "Heater-3",
url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3"
}, {
keyCode: 65,
keyTrigger: "A",
id: "Heater-4",
url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3"
}, {
keyCode: 83,
keyTrigger: "S",
id: "Clap",
url: "https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3"
}, {
keyCode: 68,
keyTrigger: "D",
id: "Open-HH",
url: "https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3"
}, {
keyCode: 90,
keyTrigger: "Z",
id: "Kick-n'-Hat",
url: "https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3"
}, {
keyCode: 88,
keyTrigger: "X",
id: "Kick",
url: "https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3"
}, {
keyCode: 67,
keyTrigger: "C",
id: "Closed-HH",
url: "https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3"
}]
, t = [{
keyCode: 81,
keyTrigger: "Q",
id: "Chord-1",
url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_1.mp3"
}, {
keyCode: 87,
keyTrigger: "W",
id: "Chord-2",
url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_2.mp3"
}, {
keyCode: 69,
keyTrigger: "E",
id: "Chord-3",
url: "https://s3.amazonaws.com/freecodecamp/drums/Chord_3.mp3"
}, {
keyCode: 65,
keyTrigger: "A",
id: "Shaker",
url: "https://s3.amazonaws.com/freecodecamp/drums/Give_us_a_light.mp3"
}, {
keyCode: 83,
keyTrigger: "S",
id: "Open-HH",
url: "https://s3.amazonaws.com/freecodecamp/drums/Dry_Ohh.mp3"
}, {
keyCode: 68,
keyTrigger: "D",
id: "Closed-HH",
url: "https://s3.amazonaws.com/freecodecamp/drums/Bld_H1.mp3"
}, {
keyCode: 90,
keyTrigger: "Z",
id: "Punchy-Kick",
url: "https://s3.amazonaws.com/freecodecamp/drums/punchy_kick_1.mp3"
}, {
keyCode: 88,
keyTrigger: "X",
id: "Side-Stick",
url: "https://s3.amazonaws.com/freecodecamp/drums/side_stick_1.mp3"
}, {
keyCode: 67,
keyTrigger: "C",
id: "Snare",
url: "https://s3.amazonaws.com/freecodecamp/drums/Brk_Snr.mp3"
},
];
function DrumMachine() {
return <div className="bg-info min-vh-100 text-white">
<div className= "text-center">
<h2>Drum Machine </h2>
{audioClips.map(clip => (
<Pad key={clip.id} clip={clip}/>
))}
</div>
</div>
}
function Pad({clip}) {
const [active,setActive] = React.useState(false);
React.useEffect(() => {
document.addEventListener("keyDown",handlePressKey);
return() => {
document.removeEventListener("keyDown",handlePressKey);
};
}, []);
const handleKeyPress = (e) => {
if (e.keyCode === clip.keyCode) {
playSound();
}
}
const playSound = () => {
const audioTag = document.getElementById(audio.keyTrigger);
setActive(true);
setTimeout(() => setActive(false), 200);
audioTag.currentTime = 0;
audioTag.play();
}
return(
<div onClick={playSound} className={'btn btn-secondary p-4 n-3 ${active && "btn-warning"}'}>
<audio className="clip" id={clip.keyTrigger} src={clip.url}/>
{clip.keyTrigger}
</div>
);
}
/*render() {
return(
<div> <div className ="drumMachineApp" id="drummachine"
<div id="display">
<div className = "drumpad" id="Q" <audio className="clip" src='https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3'></audio>
<a href="https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3"> Heater-1 </a>
</div>
<div className = "drumpad" id="W" <audio className="clip" src='https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3'></audio>
<a href="https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3"> Heater-1 </a></div>
<div className = "drumpad" id="E"></div>
<div className = "drumpad" id="A"></div>
<div className = "drumpad" id="S"></div>
<div className = "drumpad" id="D"></div>
<div className = "drumpad" id="Z"></div>
<div className = "drumpad" id="X"></div>
<div className = "drumpad" id="C"></div>
</div>
/div>
</div>
);
}
]
} */
ReactDOM.render(<DrumMachine />, document.getElementById("drummachine"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Challenge Information:
Front End Development Libraries Projects - Build a Drum Machine