-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgame.js
35 lines (23 loc) · 1.09 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var buttonColours = ["red", "blue", "green", "yellow"];
var gamePattern = [];
var userClickedPattern = [];
$(".btn").click(function() {
var userChosenColour = $(this).attr("id");
userClickedPattern.push(userChosenColour);
//1. In the same way we played sound in nextSequence() , when a user clicks on a button, the corresponding sound should be played.
playSound(userChosenColour);
});
function nextSequence() {
var randomNumber = Math.floor(Math.random() * 4);
var randomChosenColour = buttonColours[randomNumber];
gamePattern.push(randomChosenColour);
$("#" + randomChosenColour).fadeIn(100).fadeOut(100).fadeIn(100);
//4. Refactor the code in playSound() so that it will work for both playing sound in nextSequence() and when the user clicks a button.
playSound(randomChosenColour);
}
//2. Create a new function called playSound() that takes a single input parameter called name.
function playSound(name) {
//3. Take the code we used to play sound in the nextSequence() function and add it to playSound().
var audio = new Audio("sounds/" + name + ".mp3");
audio.play();
}