I'm new to coding and I'm playing around with someone's code for a Hangman game. I decided to alter the word bank to be the names of various songs. I thought it'd be cool if that song would play after guessed correctly, but I'm not sure how to code that.
Of course I'd declare and set the variable: var stayinAlive = new Audio(/assets/sounds/stayingalive.mp3);
To actually code that instance, I thought it would be something along the lines of using an if loop, such as:
if (selectableWords == "stayinalive") {
stayinAlive.play();
}
else if {
(selectableWords == "dancingqueen") {
dancingQueen.play();
Of course, that code doesn't work. I don't quite think I'm linking the right variable (selectableWords) and I'm also not sure where I would even put this piece of code (I'm guessing it might have something to do with the checkWin() function?)
Here's some of the javascript I'm working from:
'use strict';
var selectableWords =
[
"stayinalive",
"dancingqueen",
];
const maxTries = 10;
var guessedLetters = [];
var currentWordIndex;
var guessingWord = [];
var remainingGuesses = 0;
var hasFinished = false;
var wins = 0;
var losses = 0;
// Game sounds
var stayinAlive = new Audio('./assets/sounds/stayinalive.mp3');
var dancingQueen = new Audio('./assets/sounds/dancingqueen.mp3');
function resetGame() {
remainingGuesses = maxTries;
currentWordIndex = Math.floor(Math.random() * (selectableWords.length));
Later on in the code...
function checkWin() {
if(guessingWord.indexOf("_") === -1) {
document.getElementById("youwin-image").style.cssText = "display: block";
document.getElementById("pressKeyTryAgain").style.cssText= "display: block";
wins++;
hasFinished = true;
}
};
function checkLoss() {
if(remainingGuesses <= 0) {
document.getElementById("gameover-image").style.cssText = "display: block";
document.getElementById("pressKeyTryAgain").style.cssText = "display:block";
losses++
hasFinished = true;
}
}