Drum-machine with React 16.3 + (React.createRef())

Dear Coder,

I hope you all do fine.

I cant figure out why my code fails test 5 and 6. Is it because the test-script cant handle “React.createRef”, or did i made another mistake? (sound plays fine :slight_smile: )

import React, { Component } from 'react'

class Button extends Component {
  constructor(props) {
    super(props)
    this.audio = React.createRef()
  }
      handleClick = () => {
        this.props.handler(this.props.name) 
        const audio = new Audio(this.audio.current.src);
        audio.play();
      };

      handleKeyPress = (e) => {
        if (e.keyCode === this.props.keyCode) {
          this.props.handler(this.props.name) 
          const audio = new Audio(this.audio.current.src);
          audio.play();
      }};

      componentDidMount() {
        document.addEventListener('keydown', this.handleKeyPress);
      }

  render() {
    return (
      <div>
        <button className="drum-pad" id={this.props.name} style={buttonStyle} onClick={this.handleClick} onKeyPress={this.handleKeyPress}>
            <h1>{this.props.name}</h1>
            <audio ref={this.audio} src={this.props.src} className='clip' id={this.props.name} />
        </button>
      </div>
    )
  }
}

const buttonStyle = {
    fontSize: '20px',
    color: '#323334',
    backgroundColor: 'darkgrey',
    fontFamily: 'monospace',
    fontVariant: 'bold',
    width: '7em'
}

export default Button;

and:

import React, { Component } from 'react';
import Button from './button';
import Display from './display';

class Drumpad extends Component {
  constructor(props) {
    super(props);
        this.state = { 
              Display: '',
              newSound: '',
              selectedButton: "",
              Q:
              {
                src: 'http://www.vibrationdata.com/Kawai_fifth_A_E.mp3'
              },
              W:
              {
                src: 'http://dight310.byu.edu/media/audio/FreeLoops.com/5/5/Piano%20Key%20C6-2871-Free-Loops.com.mp3'
              },
              E:
              {
                src: 'http://dight310.byu.edu/media/audio/FreeLoops.com/5/5/Piano%20Key%20C3-2862-Free-Loops.com.mp3'
              },
              A:
              {
                src: 'http://www.ulfkonrad.de/sound/mp3/ton_c/klavier.mp3'
              },
              S:
              {
                src: 'http://dight310.byu.edu/media/audio/FreeLoops.com/5/5/Piano%20Key%20C-2859-Free-Loops.com.mp3'
              },
              D:
              {
                src: 'http://www.vibrationdata.com/piano_G.mp3'
              },             
              Z:
              {
                src: 'http://www.vibrationdata.com/piano_F_sharp.mp3'
              },              
              X:
              {
                src: 'http://www.vibrationdata.com/Kawai_Major_Sixth_A_F_sharp.mp3'
              },
              C:
              {
                src: 'http://www.vibrationdata.com/Kawai_octave_A_A.mp3'
              }
          }
          this.testChange = this.testChange.bind(this);
          this.selectionChanged = this.selectionChanged.bind(this);
          this.submitNewSound = this.submitNewSound.bind(this);
    }

    handler = (val) => {
      this.setState({
        Display: val
      })
    }

    testChange(e) {
      this.setState({newSound: e.target.value});
  }

  selectionChanged(e) {
    this.setState({selectedButton: e.target.value});
}

submitNewSound(e) {
  this.setState({[this.state.selectedButton]: {src: this.state.newSound}});
  this.setState({newSound: ""}); 
}

  render() {
    return (
      <div style={hauptFrame}>
        <h1>Soundmaschine</h1> 
      <div style={test}>
        <div id='row1'>
        <Button name="Q" keyCode={81} id="Piano1" src={this.state.Q.src} handler={this.handler}/>
        <Button name="W" keyCode={87} id="Piano2" src={this.state.W.src} handler={this.handler}/>
        <Button name="E" keyCode={69} id="Piano3" src={this.state.E.src} handler={this.handler}/>
        </div>
        <div id='row1'>
        <Button name="A" keyCode={65} id="Piano4" src={this.state.A.src} handler={this.handler}/>
        <Button name="S" keyCode={83} id="Piano5" src={this.state.S.src} handler={this.handler}/>
        <Button name="D" keyCode={68} id="Piano6" src={this.state.D.src} handler={this.handler}/>
        </div>
        <div id='row1'>
        <Button name="Z" keyCode={90} id="Piano7" src={this.state.Z.src} handler={this.handler}/>
        <Button name="X" keyCode={88} id="Piano8" src={this.state.X.src} handler={this.handler}/>
        <Button name="C" keyCode={67} id="Piano9" src={this.state.C.src} handler={this.handler}/>
        </div> 
        <Display className="display" cfg={this.state.Display}/>
      </div>
      <br/>
      <input type="text" name="testa" size="40" component="input" placeholder="paste in the url (has to refer to a .mp3)" onChange={this.testChange} value={this.state.newSound}/>
      <select defaultValue={""} name="buttonName" size="1" onChange={this.selectionChanged}>
      <option value="" disabled hidden></option>
      <option value="Q">Q</option>
      <option value="W">W</option>
      <option value="E">E</option>
      <option value="A">A</option>
      <option value="S">S</option>
      <option value="D">D</option>
      <option value="Z">Z</option>
      <option value="X">X</option>
      <option value="C">C</option>
    </select>
                <button type="submit" name="Submit" onClick={this.submitNewSound}>Change Sound</button>
      </div>
    )
  }
}

const hauptFrame = {
  marginLeft: '30%',
  position: 'relative',
}

const test = {
  display: 'flex'
}

export default Drumpad;

leads to:

import React, { Component } from 'react';
import './App.css';
import Drumpad from './components/drumpad';

class App extends Component {
  render() {
    return (
      <div id="drum-machine">
        <Drumpad />
      </div>
    );
  }
}

export default App;

Someone got a Idea why my wonderful solution doesn`t pass test 5 and 6?

Best regards

The error-message is as follows:

The <audio> element with id="Q" does not play when the Q .drum-pad is clicked : expected true to be false
AssertionError: The <audio> element with id="Q" does not play when the Q .drum-pad is clicked : expected true to be false
    at o.<anonymous> (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:549:15104)
    at o.e (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:425:182)
    at Object.get (<anonymous>)
    at Object.e [as get] (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:92:1380)
    at Function.n.isFalse (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:574:1349)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:657:175897
    at NodeList.forEach (<anonymous>)
    at a.<anonymous> (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:657:175839)
    at c (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:35224)
    at i.p.run (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:35154)
    at T.runTest (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41723)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:42605
    at o (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41019)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41088
    at o (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:40141)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:40906
    at f (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:593:1377)
No audio plays when the Q key is pressed : expected true to be false
AssertionError: No audio plays when the Q key is pressed : expected true to be false
    at o.<anonymous> (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:549:15104)
    at o.e (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:425:182)
    at Object.get (<anonymous>)
    at Object.e [as get] (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:92:1380)
    at Function.n.isFalse (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:574:1349)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:657:176603
    at NodeList.forEach (<anonymous>)
    at a.<anonymous> (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:657:176461)
    at c (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:35224)
    at i.p.run (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:35154)
    at T.runTest (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41723)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:42605
    at o (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41019)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:41088
    at o (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:40141)
    at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:598:40906
    at f (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:593:1377)

As a matter of fact, the audio file is played.

Best regards

It would be better if you post your code on codepen or codesandbox and share link. That way it would be easier to test it.

I didn’t run your code, but if I remember correctly, tests want explicitly for the <audio> element to be played. Try not to create a new audio using JS, but use ref and call .play() method directly.

So instead of this:

const audio = new Audio(this.audio.current.src);
audio.play();

do something like this:

this.audio.current.play()
1 Like

You are right, I passed the tests.

THANK YOU !!!