Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,813 members, 7,820,876 topics. Date: Wednesday, 08 May 2024 at 12:04 AM

Please Someone Should Help Me With This Code. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Please Someone Should Help Me With This Code. (1483 Views)

I Am Bored, I Just Tried This Code / Java Code Pls Modify This Code Help / Java Programmers ,I Need Help Fixing This Code. (2) (3) (4)

(1) (Reply) (Go Down)

Please Someone Should Help Me With This Code. by oyadanoz: 12:06pm On Sep 05, 2017
Please i am building a computer based exam, and but am having issue with my javascript file its not reflecting, please help view the source code below, please i will appreciate your comments.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
</style>
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What is 10 + 4?", "12", "14", "16", "B" ],
["What is 20 - 9?", "7", "13", "11", "C" ],
["What is 7 x 3?", "21", "24", "25", "A" ],
["What is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test"wink;
if(pos >=questions.length){
test.innerHTML = "<h2>You got "+correct" of "+questions.length+" questions correct</h2>";
_("test_status"wink.innerHTML = "Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status"wink.innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementByName("choices"wink;
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>

</html>
Re: Please Someone Should Help Me With This Code. by israelboy1(m): 4:47pm On Sep 05, 2017
1. People are ready to help, but the funniest part of it is that THEY ARE NOT ROBOT

2. Did you actually read through what you posted here, to see if you yourself understood what was actually posted by you

3. I do not think this platform actually supports posting codesthe way you actually did

4. You have stackoverflow there, and if you can't get an answer from there, you can always post the link here.. so, people here and on stackoverflow can always give you the answer

5. You can also just create a github account, post the code there, then call the attention of people here to help commit

6. I really love to help.

7. I just realize that, i am almost near helping

8. I later found out that, I can't help

Take care.

1 Like

Re: Please Someone Should Help Me With This Code. by BobAxelrod: 7:53pm On Sep 05, 2017
be more detailed, what is not reflecting in your js file?

won't it be better using jquery, vue, react etc?
Re: Please Someone Should Help Me With This Code. by klefjoxey: 8:41am On Sep 06, 2017
oyadanoz:
Please i am building a computer based exam, and but am having issue with my javascript file its not reflecting, please help view the source code below, please i will appreciate your comments.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
</style>
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What is 10 + 4?", "12", "14", "16", "B" ],
["What is 20 - 9?", "7", "13", "11", "C" ],
["What is 7 x 3?", "21", "24", "25", "A" ],
["What is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test"wink;
if(pos >=questions.length){
test.innerHTML = "<h2>You got "+correct" of "+questions.length+" questions correct</h2>";
_("test_status"wink.innerHTML = "Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status"wink.innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementByName("choices"wink;
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>

</html>
You should know this code is very ugly and it is hard to read. It is a good practice to separate your css and javascript. Please do so

1 Like

Re: Please Someone Should Help Me With This Code. by laxrt: 12:08pm On Sep 06, 2017
next time wrap your code with a [code ]your code [/code ] so that it preserves the indentation

1) there should be a '+' between 'correct' and 'of' in the part
test.innerHTML = "<h2>You got "+correct" of "+questions.length+" questions correct</h2>"


2) its getElementsByName , note the 's' after Element

3) Ever heard of the javascript console

2 Likes 1 Share

Re: Please Someone Should Help Me With This Code. by yorex2011: 6:00pm On Sep 06, 2017
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What is 10 + 4?", "12", "14", "16", "B" ],
["What is 20 - 9?", "7", "13", "11", "C" ],
["What is 7 x 3?", "21", "24", "25", "A" ],
["What is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test"wink;
if(pos >=questions.length){
test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status"wink.innerHTML = "Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status"wink.innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementByName("choices"wink;
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);

2 Likes 1 Share

Re: Please Someone Should Help Me With This Code. by ANTONINEUTRON(m): 4:54pm On Sep 07, 2017
Why can't you use php
Your source code can be viewed easily
Re: Please Someone Should Help Me With This Code. by Jaddo19(m): 5:10pm On Sep 07, 2017
Firstly,
I think u should have added comments to explain ur code better.
secondly,
What error do u get when u execute d code.
lastly,
Since its a computer based test, why not use a server-side language cuz ur source code can easily be viewed just As the person above me Just said
Re: Please Someone Should Help Me With This Code. by Nobody: 9:14am On Sep 08, 2017
Na sorcery una dey do here? Una wan blow my head
Re: Please Someone Should Help Me With This Code. by holuphisayor(m): 11:48am On Sep 08, 2017
create a codepen account and post link to ur .js file here
Re: Please Someone Should Help Me With This Code. by phainix: 6:46pm On Sep 08, 2017
laxrt:
next time wrap your code with a [code ]your code [/code ] so that it preserves the indentation

1) there should be a '+' between 'correct' and 'of' in the part
test.innerHTML = "<h2>You got "+correct" of "+questions.length+" questions correct</h2>"


2) its getElementsByName , note the 's' after Element

3) Ever heard of the javascript console

I hope you were able to resolve your issue with this comment because listed here are all your errors, here is a corrected version


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
div#test{ border:#000 1px solid; padding:10px 40px 40px 40px; }
</style>
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What is 10 + 4?", "12", "14", "16", "B" ],
["What is 20 - 9?", "7", "13", "11", "C" ],
["What is 7 x 3?", "21", "24", "25", "A" ],
["What is 8 / 2?", "10", "2", "4", "C" ]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion(){
test = _("test"wink;
if(pos >=questions.length){
test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
_("test_status"wink.innerHTML = "Test completed";
pos = 0;
correct = 0;
return false;
}
_("test_status"wink.innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' value='A'> "+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='B'> "+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' value='C'> "+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices"wink;
for(var i=0; i<choices.length; i++){
if(choices[i].checked){
choice = choices[i].value;
}
}
if(choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
</head>
<body>
<h2 id="test_status"></h2>
<div id="test"></div>
</body>

</html>

2 Likes

(1) (Reply)

2019 Google Hash Code Programming Challenge For Professionals In Africa / How Can I Make Money With My Radio App / ..

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 45
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.