Jump to content

Recommended Posts

Hi! 

So I am new to coding and I am just trying to make a small project for myself. All it is a button that when you press it, it plays randomly one of three sounds. Something is going wrong though and I can't figure out what, anyone know? 

 

<!doctype <!DOCTYPE html>
<link rel="stylesheet" href="C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Code\Random Sounds.css">


<h1> Random Sounds </h1>

<button id="press"> Press me! </button>


<script>
var num = 0

function pressed() {
  num = Math.floor(1 + 3 * Math.random())
  if (num == 1) {
    var audio1 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('1')
    audio1.play()
  } else if (num == 2) {
    var audio2 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('2')
    audio2.play()
  } else {
    var audio3 = new Audio('C:\Users\Owner\Google Drive\Code\Experiments\HTML-CSS-JAVASCRIPT\Random Sound\Sounds\OFFICIAL Somewhere over the Rainbow - Israel IZ Kamakawiwoʻole.mp3');
    console.log('3')
    audio3.play()
  }
}
press.onclick = pressed


</script>

 

 

cheers 

 

A

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...