# Need help with logic

• Professor
• Posts: 854

3+ Months Ago

My assignment for this week is like this:
• Write a test scoring script that will receive five answers through a prompt.
• The answers will be either "t" for true, or "f" for false.
• Use 3 arrays, corresponding to each other. One which holds the questions, one that will be dimensioned with the answers given, and one that holds the true answers.
• Compare the answers given to the answer key and create a percentage based grade that will be outputted on the page.
• Use the "continue" command to control the course of action if a user clicks "cancel".
So I've got the first 2 arrays, the prompts with the questions, and the answer key. I'm not sure why I would need a 3rd array...wouldn't it just be a copy of the first array?
I have absolutely no idea how to use the "continue" command. I checked w3schools and didn't get much info from that.
I assume the best thing to do is a while loop?

What's the best way to set this up?
• Professor
• Posts: 854

3+ Months Ago

okay, I think I got most of it...but it's not working. Not sure why.
No matter what I input, it always shows the alert from my second else if statement, stating that it's invalid input.

JAVASCRIPT Code: [ Select ]
var i = 0;
while(i<=4){
var ques = new Array();
ques[0] = 'Two plus two is ten.';
ques[1] = 'George Washington was the first U.S. president.';
ques[2] = 'Al Gore is our current Vice President.';
ques[3] = 'Two plus Two is four.';
ques[4] = 'You are not an alien from Mars.';

var ans = new Array();
ans[i] = prompt(ques[i],'');

var key = new Array();
key[0] = 'F';
key[1] = 'T';
key[2] = 'F';
key[3] = 'T';
key[4] = 'T';

var score = 0;

if(ans[i] == null){
break;
}
else if(ques[i].toUpperCase() == key[i].toUpperCase()) {
score = score+20;
}
else if(ques[i].toUpperCase() != 'F' && ques[i].toUpperCase() != 'T'){
alert('You did not input a valid answer. Please Enter either "F" for false, or "T" for true.');
ans[i];
}
else {
i++
}
}

1. var i = 0;
2. while(i<=4){
3. var ques = new Array();
4.    ques[0] = 'Two plus two is ten.';
5.    ques[1] = 'George Washington was the first U.S. president.';
6.    ques[2] = 'Al Gore is our current Vice President.';
7.    ques[3] = 'Two plus Two is four.';
8.    ques[4] = 'You are not an alien from Mars.';
9.
10. var ans = new Array();
11.    ans[i] = prompt(ques[i],'');
12.
13. var key = new Array();
14.    key[0] = 'F';
15.    key[1] = 'T';
16.    key[2] = 'F';
17.    key[3] = 'T';
18.    key[4] = 'T';
19.
20. var score = 0;
21.
22. if(ans[i] == null){
23.       break;
24.    }
25. else if(ques[i].toUpperCase() == key[i].toUpperCase()) {
26.    score = score+20;
27. }
28. else if(ques[i].toUpperCase() != 'F' && ques[i].toUpperCase() != 'T'){
29.    alert('You did not input a valid answer. Please Enter either "F" for false, or "T" for true.');
30.    ans[i];
31. }
32. else {
33.    i++
34. }
35. }
36.
38.
• ٩๏̯͡๏۶
• Posts: 3422
• Loc: Richland, WA

3+ Months Ago

• Professor
• Posts: 854

3+ Months Ago

Ah jeez, can't believe it was that simple.

## Post Information

• Total Posts in this topic: 4 posts
• Users browsing this forum: No registered users and 88 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum