Below is the Code I have, I need it to execute the following two things: 1. Ther
ID: 3814628 • Letter: B
Question
Below is the Code I have, I need it to execute the following two things:
1. There are 12 questions listed in the code, the quiz is only supposed to ask 8 questions,which it does, the problem is, it is only asking the first 8 questions in the code in random order, I want to add more questions to the code, but it's pointless if it will only ask the first 8 questions listed in the code, so I need that helped with
2. I have a folder listed called "Homework 11 Pictures" it has many random nothing pictures in it, I need this to where if the user gets 5 out of 8 correct on the test, one of those pictures in the folder pops up at the end available to save
<!DOCTYPE html>
<head>
<title>HW 11</title>
<style>
div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
</style>
</head>
<body>
<div>
<h2 id="test_status">Do Yo Best </h2>
<h3 id="timeleft">Time left</h3>
</h4> I Finish Fast, So Can You</h4>
</div>
<div id="test"></div>
<script>
var totalQuetions = 08;
var myVar;
function startTimer() {
myVar = setInterval(function(){myTimer()},1000);
timelimit = maxtimelimit;
}
function myTimer() {
if (timelimit > 0) {
curmin=Math.floor(timelimit/60);
cursec=timelimit%60;
if (curmin!=0) { curtime=curmin+" minutes and "+cursec+" seconds left"; }
else { curtime=cursec+" seconds left"; }
$_('timeleft').innerHTML = curtime;
} else {
$_('timeleft').innerHTML = timelimit+' - Out of Time - You Took The L Homie';
clearInterval(myVar);
}
timelimit--; //How Much Time
}
var pos = 0, posn, choice, correct = 0, rscore = 0;
var maxtimelimit = 15, timelimit = maxtimelimit; // 15 seconds per question
var questions = [
[ "Who is the first offical President of the U.S.A?", "Ben Franklin", "George Washington", "Barack Obama", "Abe Lincoln", "Jay-Z", "B" ],
[ "Which Movie won best picture at the 71st Academy Awards", "Titanic", "American Beauty", "Shakespeare in Love", "Gladiator", "Pootie Tang", "C" ],
[ "More people die of dehydration than drown in a desert?", "LIE", "Troof", "L", "Taco", "Lice", "A" ],
[ "Which was the northern most battle of the civil war?", "Cedar Hill", "Bull Run", "Gettysburg", "Antietam", "Schrute Farms", "E" ],
[ "What is 99 x 99?", "9907", "9571", "9801", "8901", "9081", "C"],
[ "The month of July is named after Julius Ceaser, what was it known as before ? ?", "Dromas", "Quintilis", "Andromeda", "Gemmae", "Junius", "B" ],
[ "The Anglo-Zanzibar war of 1896 lasted how long?", "Thirty-Eight Minutes", "Two Hours and 12 Minutes", "ninety-three minutes", "49 minutes", "2700 Seconds", "A" ],
[ "The term Dog Days of Summer comes from a star in what Constellation ?", "Coma Berenices", "Ursa Major", "Andromeda", "Corvus", "Canis Major", "E" ],
[ "Eminem was the highest selling artist of the 2000s, who was second?", "NSYNC", "The Beatles", "Beyonce", "Elvis Pressley", "Britney Spears", "B" ],
[ "The decimal value of 01000011 is", "23", "67", "48", "73", "55", "B" ],
[ "At over 2000 km long, The BLANK reef is the largest living structure on Earth", "Belize Barrier Reef", "Apo Reef", "New Caledonian", "Tubbataha", "Great Barrier Reef", "E" ],
[ "What is 842 / 2?", "407", "477", "421", "441", "389", "C" ],
["Which former WWE Suprtstar had the longest WWE Championship reign of all time?", "Gorgeous George", "Bruno Sammartino", "Andre tthe Giant", "Hulk Hogan", "Roddy Piper", "B" ],
["What was the first song to hit Number 1 on the Billboard Hot 100?", "Hit the Road Jack", "It's Only Make Believe", "The Chipmunk Song", "Little Star", "Poor Little Fool", "E" ],
];
var questionOrder = [];
function setQuestionOrder() {
questionOrder.length = 0;
for (var i=0; i<totalQuetions; i++) { questionOrder.push(i); }
questionOrder.sort(randOrd); // alert(questionOrder); // shuffle display order
pos = 0; posn = questionOrder[pos];
}
function $_(IDS) { return document.getElementById(IDS); }
function randOrd() { return (Math.round(Math.random())-0.5); }
function renderResults(){
var test = $_("test");
test.innerHTML = "<h2>You got "+correct+" of "+totalQuetions+" questions correct</h2>";
$_("test_status").innerHTML = "Test Completed";
$_('timeleft').innerHTML = '';
test.innerHTML += '<button>Re-test</a> ';
setQuestionOrder();
correct = 0;
clearInterval(myVar);
return false;
}
function renderQuestion() {
var test = $_("test");
$_("test_status").innerHTML = "Question "+(pos+1)+" of "+totalQuetions;
if (rscore != 0) { $_("test_status").innerHTML += '<br>Currently: '+(correct/rscore*100).toFixed(0)+'% correct'; }
var question = questions[posn][0];
var chA = questions[posn][1];
var chB = questions[posn][2];
var chC = questions[posn][3];
var chD = questions[posn][4];
var chE = questions[posn][5];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<label><input type='radio' name='choices' value='A'> "+chA+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='B'> "+chB+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='C'> "+chC+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='D'> "+chD+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='E'> "+chE+"</label><br>";
test.innerHTML += "<button>Submit Answer</button>";
timelimit = maxtimelimit;
clearInterval(myVar);
startTimer();
}
function checkAnswer(){
var choices = document.getElementsByName("choices");
for (var i=0; i<choices.length; i++) {
if (choices[i].checked) { choice = choices[i].value; }
}
rscore++;
if (choice == questions[posn][6] && timelimit > 0) { correct++; }
pos++; posn = questionOrder[pos];
if (pos < totalQuetions) { renderQuestion(); } else { renderResults(); }
}
window.onload = function() {
setQuestionOrder();
renderQuestion();
}
</script>
</body>
</html>
Explanation / Answer
Look for new function chooseRandom and study related code to my function. Great effort in your work though.
<!DOCTYPE html>
<head>
<title>
HW 11
</title>
<style>
div#test {
border:#000 1px solid;
padding:10px 40px 40px 40px;
}
</style>
</head>
<body>
<div>
<h2 id="test_status">
Do Yo Best
</h2>
<h3 id="timeleft">
Time left
</h3>
</h4>
I Finish Fast, So Can You
</h4>
</div>
<div id="test"></div>
<script>
var totalQuetions = 8;
var myVar;
function startTimer() {
myVar = setInterval(function(){myTimer()},1000);
timelimit = maxtimelimit;
}
function myTimer() {
if (timelimit > 0) {
curmin = Math.floor(timelimit / 60);
cursec = timelimit % 60;
if (curmin != 0)
curtime = curmin + " minutes and " + cursec + " seconds left";
else
curtime = cursec + " seconds left";
$_('timeleft').innerHTML = curtime;
} else {
$_('timeleft').innerHTML = timelimit +
' - Out of Time - You Took The L Homie';
clearInterval(myVar);
}
timelimit--; //How Much Time
}
var pos = 0, posn, choice, correct = 0, rscore = 0;
var maxtimelimit = 15, timelimit = maxtimelimit; // 15 seconds per question
var questions = [
[
"Who is the first offical President of the U.S.A?",
"Ben Franklin", "George Washington", "Barack Obama",
"Abe Lincoln", "Jay-Z", "B"
],
[
"Which Movie won best picture at the 71st Academy Awards",
"Titanic", "American Beauty", "Shakespeare in Love",
"Gladiator", "Pootie Tang", "C"
],
[
"More people die of dehydration than drown in a desert?",
"LIE", "Troof", "L", "Taco", "Lice", "A"
],
[ "Which was the northern most battle of the civil war?",
"Cedar Hill", "Bull Run", "Gettysburg", "Antietam",
"Schrute Farms", "E"
],
[ "What is 99 x 99?", "9907", "9571", "9801", "8901", "9081", "C"],
[
"The month of July is named after Julius Ceaser, " +
"what was it known as before ? ?", "Dromas", "Quintilis",
"Andromeda", "Gemmae", "Junius", "B"
],
[
"The Anglo-Zanzibar war of 1896 lasted how long?",
"Thirty-Eight Minutes", "Two Hours and 12 Minutes",
"ninety-three minutes", "49 minutes", "2700 Seconds", "A"
],
[
"The term Dog Days of Summer comes from a star in what Constellation ?",
"Coma Berenices", "Ursa Major", "Andromeda", "Corvus", "Canis Major",
"E"
],
[
"Eminem was the highest selling artist of the 2000s, who was second?",
"NSYNC", "The Beatles", "Beyonce", "Elvis Pressley",
"Britney Spears", "B"
],
[
"The decimal value of 01000011 is", "23", "67", "48",
"73", "55", "B"
],
[
"At over 2000 km long, The BLANK reef is the largest living structure"
+ " on Earth", "Belize Barrier Reef", "Apo Reef", "New Caledonian",
"Tubbataha", "Great Barrier Reef", "E"
],
[ "What is 842 / 2?", "407", "477", "421", "441", "389", "C" ],
[
"Which former WWE Suprtstar had the longest WWE Championship reign" +
" of all time?", "Gorgeous George", "Bruno Sammartino",
"Andre tthe Giant", "Hulk Hogan", "Roddy Piper", "B"
],
[
"What was the first song to hit Number 1 on the Billboard Hot 100?",
"Hit the Road Jack", "It's Only Make Believe", "The Chipmunk Song",
"Little Star", "Poor Little Fool", "E"
]
];
var questionOrder = [];
/**
* Returns true incase an array contains a given element.
* @param { Number } num - Element find.
* @returns { Boolean } True incase the given number is found.
*/
Array.prototype.contains = function (num) {
for (let k = 0; k < this.length; ++k) {
if (this[k] === num) return true;
}
return false;
};
/**
* Chooses a numNeeded number of random numbers between the range of 1 to
* maxRange.
* @param { Number } numNeeded - Number of random numbers needed.
* @param { Number } maxRange - Maximum limit of numbers to choose from.
* @returns { Array } An array of random numbers.
*/
var chooseRandom = function(numNeeded, maxRange) {
let arr = [];
let thisTime = 0;
for (let i = 0; i < numNeeded; ++i) {
do {
thisTime = Math.round(Math.random() * maxRange+1);
} while (arr.contains(thisTime));
arr.push(thisTime);
}
return arr;
};
function setQuestionOrder() {
// RJ EDIT, We are going to choose a random of 8 numbers between 0 and 11
questionOrder = chooseRandom(8, 12);
questionOrder.sort(randOrd); // alert(questionOrder);
pos = 0; posn = questionOrder[pos];
}
function $_(IDS) { return document.getElementById(IDS); }
function randOrd() { return (Math.round(Math.random())-0.5); }
function renderResults(){
var test = $_("test");
test.innerHTML = "<h2>You got "+correct+" of "+totalQuetions+" questions correct</h2>";
$_("test_status").innerHTML = "Test Completed";
$_('timeleft').innerHTML = '';
test.innerHTML += '<button>Re-test</a> ';
setQuestionOrder();
correct = 0;
clearInterval(myVar);
return false;
}
function renderQuestion() {
var test = $_("test");
$_("test_status").innerHTML = "Question "+(pos+1)+" of "+totalQuetions;
if (rscore != 0) { $_("test_status").innerHTML += '<br>Currently: '+(correct/rscore*100).toFixed(0)+'% correct'; }
var question = questions[posn][0];
var chA = questions[posn][1];
var chB = questions[posn][2];
var chC = questions[posn][3];
var chD = questions[posn][4];
var chE = questions[posn][5];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<label><input type='radio' name='choices' value='A'> "+chA+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='B'> "+chB+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='C'> "+chC+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='D'> "+chD+"</label><br>";
test.innerHTML += "<label><input type='radio' name='choices' value='E'> "+chE+"</label><br>";
test.innerHTML += "<button>Submit Answer</button>";
timelimit = maxtimelimit;
clearInterval(myVar);
startTimer();
}
function checkAnswer(){
var choices = document.getElementsByName("choices");
for (var i=0; i<choices.length; i++) {
if (choices[i].checked) { choice = choices[i].value; }
}
rscore++;
if (choice == questions[posn][6] && timelimit > 0) { correct++; }
pos++; posn = questionOrder[pos];
if (pos < totalQuetions) { renderQuestion(); } else { renderResults(); }
}
window.onload = function() {
setQuestionOrder();
renderQuestion();
}
</script>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.