This code is for a simple language quiz. It fetches two words and related audio files via a JSON call, presents the user with an image that matches one of the words, and challenges the user to make the match. My code works but it's a little repetitious and ugly. I'd like to see how the experts would propose rewriting this.
function getNewWords() {
{
var Category = "animals";
var BaseURL = "http://localhost:61741/VocabGame/play?cat=";
var URL = BaseURL + Category;
$.getJSON(URL, {
tagmode: "any",
format: "json"
}, function (data) {
var choiceA = data.nouns[0].Pinyin;
var choiceB = data.nouns[1].Pinyin;
$('#ChoiceA').html(choiceA);
$('#ChoiceB').html(choiceB);
var root = "../../Content/Audio/";
var mp31 = root + data.nouns[0].Audio1 + ".mp3";
var ogg1 = root + data.nouns[0].Audio1 + ".ogg";
var mp32 = root + data.nouns[1].Audio1 + ".mp3";
var ogg2 = root + data.nouns[1].Audio1 + ".ogg";
attachMouseEnter(mp31, ogg1, mp32, ogg2);
var random = Math.random();
if (random >= 0.5) {
correctAnswer = "ChoiceA";
Search(data.nouns[0].English);
}
else {
correctAnswer = "ChoiceB"
Search(data.nouns[1].English);
}
});
}
};
function playAudio1(mp31, ogg1) {
var mp3_src = mp31;
var oga_src = ogg1;
$('#jquery_jplayer_1').jPlayer('setMedia', {
oga: oga_src,
mp3: mp3_src
});
$('#jquery_jplayer_1').jPlayer("play");
};
function playAudio2(mp32, ogg2) {
var mp3_src = mp32;
var oga_src = ogg2;
$('#jquery_jplayer_1').jPlayer('setMedia', {
oga: oga_src,
mp3: mp3_src
});
$('#jquery_jplayer_1').jPlayer("play");
};
function attachMouseEnter(mp31, ogg1, mp32, ogg2) {
$('#ChoiceA').die('mouseenter.audio1event');
$('#ChoiceA').live('mouseenter.audio1event', function () {
playAudio1(mp31, ogg1);
});
$('#ChoiceB').die('mouseenter.audio2event');
$('#ChoiceB').live('mouseenter.audio2event', function () {
playAudio2(mp32, ogg2);
});
}