所以我有一个基于游戏(石头剪刀布)的小应用程序,但我想返回带有获胜者和音效的文本......所以我有 2 个小 mp3 文件 applause.mp3 和 fail.mp3 我该怎么做返回文本并播放文件:(?我尝试了几件事,但它告诉我“未定义”或其他错误...... :(或者它返回给我的文本购买我听不到声音..
var compare = function(choice1) {
var winSound = document.getElementById('./sounds/Applause.mp3');
var lostSound = document.getElementById('./sounds/Fail.mp3');
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
if (choice1 === computerChoice) {
return('The result is a tie!');
}
else if (choice1 === 'rock') {
if (computerChoice === 'scissors') {
result = 'You are the winner.' + winSound.play();
} else {
return 'Computer is the winner.';
}
}
else if (choice1 === 'paper') {
if (computerChoice === 'rock') {
return 'You are the winner.';
} else {
return 'Computer is the winner.';
}
}
else {
if (computerChoice === 'rock') {
return 'You are the winner.';
} else {
return 'Computer is the winner.';
}
}
};
var game = function(choice1) {
document.getElementById('result').innerHTML = compare(choice1);
};