Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<div>
  <br>
  <h1>Drum machine</h1>
  <h2>Tap a letter to play a sound</h2>
</div>
<div id="root"></div>
              
            
!

CSS

              
                h1 {
  text-align: center;
}

h2 {
  text-align: center;
  font-size: 16px;
}
              
            
!

JS

              
                //*--Set the drumpad letter, sound and sound description--*//
 const pads = [
   { id: "Low-snare", letter: "Q", src: "http://www.phatdrumloops.com/audio/wav/unitbeat.wav", color: "blue"},    
   { id: "Fast drums", letter: "W", src: "http://www.phatdrumloops.com/audio/wav/souldrums.wav", color: "chartreuse"},
       { id: "Funky beat", letter: "E", src: "http://www.phatdrumloops.com/audio/wav/babydntcry.wav", color: "yellow"},
       { id: "Sexy beat", letter: "A", src: "http://www.phatdrumloops.com/audio/acm/funky_drummer.wav", color: "blue"},
       { id: "Tinny drum", letter: "S", src: "http://www.phatdrumloops.com/audio/wav/funkyjunk.wav", color: "green"},
       { id: "Drum with cowbell", letter: "D", src: "http://www.phatdrumloops.com/audio/acm/worm.wav", color: "yellow"},
       { id: "70s beat", letter: "Z", src: "http://www.phatdrumloops.com/audio/acm/scorpio_dc.wav", color: "blue"},
       { id: "Aerosmith beat", letter: "X", src: "http://www.phatdrumloops.com/audio/acm/walk_this_way.wav", color: "green" },
       { id: "Deep drums", letter: "C", src: "http://www.phatdrumloops.com/audio/acm/soul_chicken.wav", color: "yellow"},
];

//*--Create a React component for the drumpads--*//
class DrumPad extends React.Component {
  
  componentDidMount() {

//*--Listen for keyboard press of drumpad letter--*//
 document.addEventListener('keydown', this.handleKeyDown)
  }
  
  componentWillUnMount() {

 //*--Remove listener for keyboard press of drumpad letter--*//
    document.removeEventListener('keydown', this.handleKeyDown)
  }
  
  //*Play correct sound when letter on keyboard is pressed*//
  handleKeyDown = event => {
    if (event.keyCode === this.props.letter.charCodeAt()) {
      this.audio.play()
      this.audio.currentTime=0
      this.props.handleDisplay(this.props.id)
    }
  }
  
  //*Play correct sound when drumpad on screen is clicked*//
  handleClick = () => {
    this.audio.play()
    this.audio.currentTime=0
    this.props.handleDisplay(this.props.id)
  }  


  render() {
  
  //*Styling for drumpads*//
  const drumPadStyle = {
    border: "3px solid black",
    heigh: "100px",
    width: "100px",
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    padding: "10px",
    fontSize: "10px",
    marginLeft: "50px",
    fontWeight: "bold",
    borderRadius: "20px",
    backgroundColor: "chartreuse"
  };
    
  const drumPadStyle2 = {
    border: "3px solid black",
    heigh: "100px",
    width: "100px",
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    padding: "10px",
    fontSize: "10px",
    marginLeft: "50px",
    fontWeight: "bold",
    borderRadius: "20px",
    backgroundColor: "DeepSkyBlue"
  };
    
  const drumPadStyle3 = {
    border: "3px solid black",
    heigh: "100px",
    width: "100px",
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    padding: "10px",
    fontSize: "30px",
    marginLeft: "50px",
    fontWeight: "bold",
    borderRadius: "20px",
    backgroundColor: "pink"
  };
  
  const drumPadStyle4 = {
    border: "3px solid black",
    heigh: "100px",
    width: "100px",
    display: "flex",
    justifyContent: "center",
    alignItems: "center",
    padding: "10px",
    fontSize: "10px",
    marginLeft: "50px",
    fontWeight: "bold",
    borderRadius: "20px",
    backgroundColor: "yellow"
  };
  
  return (
    <div 
      className="drum-pad" 
      id={this.props.id}  
      style={(this.props.letter === "Q") || (this.props.letter === "S") || (this.props.letter === "C") ? drumPadStyle2 
      : (this.props.letter === "W") || (this.props.letter === "D") ? drumPadStyle3
      : (this.props.letter === "E") || (this.props.letter === "Z")? drumPadStyle4
      : drumPadStyle}
      onClick={this.handleClick}
    >
      
      <h1>{this.props.letter}</h1>
      <audio 
        ref={ref => this.audio = ref}
        className="clip"
        src={this.props.src} 
        id={this.props.letter}>
      </audio>
    </div>
  );
}
}

//*Create React component for outer drum machine*//
class App extends React.Component {
  
  constructor(props){
    super(props)
    this.state = {
      display: ''
    }
  }
  
  handleDisplay = display => this.setState({ display })
  
  render() {
    
    const machineStyle = {
      border: "3px solid black",
      marginTop: "5vh",
      width: "600px",
      height: "500px",
      paddingLeft: "40px",
      paddingRight: "40px",
      backgroundColor: "grey",
    }
    
    
    const drumPadsStyle = {
    height: "60vh",
    width: "90vh",
    display: "flex",
    flexWrap: "wrap",
    justifyContent: "right",
    alignItems: "center",
    marginLeft: "3px",
    }
   
   //*Set styling for sound description box and text*//
   const descriptionStyle = {
    justifyContent: "center",
    textAlign: "center",
    paddingTop: "0.5vh",
   }
    
   const descriptionBox = {
     backgroundColor: "white",
     height: "10vh",
     width: "410px",
     marginLeft: "5vh",
     marginRight: "20vh",
     marginBottom: "10px",
     border: "3px solid black",
     borderRadius: "20px"
   }
   
   
   const description = {
     fontSize: "1.5em", 
     paddingTop: "2px",
     alignItems: "center",
     justifyContent: "center",
   }
   
   
     return (
  //*Add drum pad component and description box to drum pad component *//
   <div id="drum-machine" className="container" style={machineStyle}>
         <div className="row">
         <div className="col-sm-12">
     <div id="drum-pads" style={drumPadsStyle}>
       {pads.map(d => (
         <DrumPad
             id={d.id}
             letter={d.letter}
             src={d.src}
             handleDisplay={this.handleDisplay}
           />
       ))}
      </div>
      </div>
      </div>
      <div className="col-sm-12">
        <div style={descriptionStyle}>
        <div style={descriptionBox}>
        <div id="display" style={description}><p>  {this.state.display}</p></div>
        </div>
        </div>
        </div>
      </div>
 );
}
}

ReactDOM.render(
  <App />, 
document.getElementById("root")
);
              
            
!
999px

Console