1

I am writing code for the Old McDonald song. I have 3 animals as an option for the user. Based on their input, I have written code so that the song proceeds with the animal sound as text output. I would also like to add a sound for the onclick event, but have it change with each animal. Is this possible?

Here is the code so far:

<!DOCTYPE html>


<script>
function songFunction() {
    var output;
    var animal = prompt("Choose an animal. Cow, chicken, or sheep?", "Enter 
an animal.");

    if (animal == null) {
        output = "You don't want to sing along?";
    } 
    else if (animal.toUpperCase() == "COW") {
        output = "With a moo moo here, and a moo moo there. Here a moo, 
there a moo, everywhere a moo moo!";
    }
    else if (animal.toUpperCase() == "CHICKEN") {
        output = "With a cluck cluck here, and a cluck cluck there. Here a 
cluck, there a cluck, everywhere a cluck cluck!";
    }
    else if (animal.toUpperCase() == "SHEEP") {
        output = "With a baa baa here, and a baa baa there. Here a baa, 
there a baa, everywhere a baa baa!"
    }
    else {
        output = "This farm only has 3 kinds of animals. Please try again."
    }

    document.getElementById("message").innerHTML = output;
}
</script>

<html>
<head>
<title>Old McDonald's Farm</title>
</head>

<body>

<p>Old McDonald had a farm. And, on his farm he had a....</p>
<button onclick="songFunction()">What comes next?</button>

<p id="message"></p>
</body>
</html>
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • You could crop the songs according to your need and play using the play method in javascript `https://www.w3schools.com/jsref/met_audio_play.asp` or record time stamp according to your need and change current time as required or if you don't need music and just want to convert string to audio, you can use speech synthesis `https://developer.mozilla.org/en-US/docs/Web/API/Window/speechSynthesis` – suman Dec 13 '17 at 01:44
  • https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Video_and_audio_APIs – Derek 朕會功夫 Dec 13 '17 at 01:48

0 Answers0