I know this is a broad question but I'm still novice to know what to search/ask for.
I have this JavaScript series for a Jeopardy game. I am sure that there is a better way to do this because there is so much repetition. I'm thinking something with some variables, arrays, and/or passing IDs through when the function is called but don't know where to start. Any ideas would be appreciated. I am not asking anyone to do the work, just give me some ideas and examples of which direction to go.
var score = 0;
//Disable the question button
function disableButton(btnID){
document.getElementById(btnID.id).disabled = true;
}
//Show current score
function endQuestion(){
alert ("Your total score is now " +score);
}
//Toggle Images On
function showImgBtn1(){
document.getElementById('btn1pic').style.visibility = 'visible';
setTimeout(askQuestion1,3000);
}
function showImgBtn2(){
document.getElementById('btn2pic').style.visibility = 'visible';
setTimeout(askQuestion2,3000);
}
//This keeps going for every question--repeated 9 to 20 times
//Questions
function askQuestion1()
{
var answer = prompt ("What body system contains the heart, blood, arteries, and veins?") .toLowerCase();
if (answer==="circulatory") {
alert ("Correct!");
score += 100;
}
else {
alert ("Sorry, incorrect.");
score -= 100;
}
endQuestion();
document.getElementById('btn1pic').style.visibility = 'hidden';
}
function askQuestion2()
{
var answer = prompt ("What body system contains the brain, spinal cord, and nerves?") .toLowerCase();
if (answer==="nervous") {
alert ("Correct!");
score += 200;
}
else {
alert ("Sorry, incorrect.");
score -= 200;
}
endQuestion();
document.getElementById('btn2pic').style.visibility = 'hidden';
}
//This keeps going for every question--same code, just replace the question and answer repeated 9 to 20 times
This is how I call it on my HTML page:
<td><button id="btn1" onclick="showImgBtn1();disableButton(btn1)">100</button></td> //each button has a successive ID
and this is how I have the pics set up on my HTML page:
<div>
<img class="picClue" id="btn1pic" src="btn1.jpg" height="200px">
<img class="picClue" id="btn2pic" src="btn2.jpg" height="200px">
<img class="picClue" id="btn3pic" src="btn3.jpg" height="200px">
<img class="picClue" id="btn4pic" src="btn4.jpg" height="200px">
</div>
Thanks for any advice!
showImgBtn(button, askQuestion, duration)- Then do this in a similar way withaskQuestion(questionId)and so on..