Farmer Game Code

<html>
<head>
<script>
window.onload = init;

function init () {
var animalList = [“pig”, “cow”, “horse”, “duck”];
var soundsList = [“oink”, “moo”, “neigh”, “quack”];
var randomNum = Math.floor(Math.random() * animalList.length);

var animal = animalList[randomNum];
var sound = soundsList[randomNum];

var soundPlace = document.getElementById(“soundType”);
soundPlace.innerHTML = sound;

var button = document.getElementById(“answrButton”);
button.onclick = function(){var feedback = playGame(animal);
var output = document.getElementById(“feedback”);

if (feedback == “correct”) {
output.innerHTML = “You guessed the animal correctly!”;
} else {
output.innerHTML = “Sorry that wasn’t right”;
}
};

}

function playGame(animal){
var usrGuess = document.getElementById(“answr”).value;
var solution = “”;
if(usrGuess == animal){
solution = “correct”;
} else {
solution = “incorrect”;
}

return solution;
}
</script>
</head>
<body>
<p>What animal makes a <span id=”soundType”></span> noise?</p>
<input type=”text” id=”answr” /><input type=”button” id=”answrButton” />
<p id=”feedback”></p>
</body>
</html>

Comments are closed.