So I made this small, short childish quiz just to practice javascript skills since im a newbie here. This is the code:
<! DOCTYPE>
<html>
<head>
<title>Interactive quiz</title>
</head>
<body>
<input type="button" value="Start the quiz" name="quiz" onclick="durationOfQuiz();">
<script>
var questions = ["How many times did 50 cent get shot? a)9 b)4 c)3",
"What year did Micheal Jackson die? a)2009 b) 2012 c) 2010",
"How many awards has Drake recieved as of January 2015? a)34 b)36 c) 20",
"What year did Kanye West win his first award? a) 2004 b) 2005 c )2003",
"How old is 50 cent? a)45 b)38 c )39",
"What year did the beatles break up? a)1987 b)1970 c)1966",
"How many sons does 50 cent have? a)4 b)3 c)2",
"When did 2Pac die? a)1996 b)1990 c)1991",
"When did biggie smalls die? a)1997 b)1980 d) 1990",
"when did eazy e die? a)1995 b)1996 c) 1989"];
var userAnswers = [];
var pointCounter = 0;
function durationOfQuiz(){
//TIMER GOES HERE
//gettingAnswers();
setTimeout(gettingAnswers,0);
//ENDING TIMER
}
function gettingAnswers (){
for(var i = 0; i < questions.length; i++){
var answers = prompt(questions[i]);
userAnswers[i] = answers;
}
if(userAnswers[0] == "9"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[0] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[0] + " You typed in " +userAnswers[0] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[1] == "2012"){document.write("<p>" + questions[1] + " You typed in " +userAnswers[1] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[1] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[1] + " You typed in " +userAnswers[1] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[2] == "36"){document.write("<p>" + questions[2] + " You typed in " +userAnswers[2] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[2] == "b"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[2] + " You typed in " +userAnswers[2] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[3] == "2004"){document.write("<p>" + questions[3] + " You typed in " +userAnswers[3] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[3] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[3] + " You typed in " +userAnswers[3] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[4] == "39"){document.write("<p>" + questions[4] + " You typed in " +userAnswers[4] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[4] == "c"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[4] + " You typed in " +userAnswers[4] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[5] == "1970"){document.write("<p>" + questions[5] + " You typed in " +userAnswers[5] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[5] == "b"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[5] + " You typed in " +userAnswers[5] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[6] == "2"){document.write("<p>" + questions[6] + " You typed in " +userAnswers[6] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[6] == "c"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[6] + " You typed in " +userAnswers[6] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[7] == "1996"){document.write("<p>" + questions[7] + " You typed in " +userAnswers[7] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[7] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[7] + " You typed in " +userAnswers[7] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[8] == "1997"){document.write("<p>" + questions[8] + " You typed in " +userAnswers[8] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[8] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[8] + " You typed in " +userAnswers[8] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
if(userAnswers[9] == "1995"){document.write("<p>" + questions[9] + " You typed in " +userAnswers[9] + ". Correct! +2 points! " + "</p>"); pointCounter = pointCounter + 2;
}else if(userAnswers[9] == "a"){document.write("<p><span class = 'correct'>" + questions[0] + " You typed in " +userAnswers[0] + ". Correct! +2 points! " + "</span></p>");pointCounter = pointCounter + 2;}
else{document.write("<p>" + questions[9] + " You typed in " +userAnswers[9] + ". Incorrect! -1 points! " + "</p>"); pointCounter = pointCounter - 1;}
document.write("Total point = " + pointCounter);
if(pointCounter<5){
document.body.style.backgroundColor="grey";
}else if(pointCounter > 6 && pointCounter <10){
document.body.style.backgroundColor="pink";
}else if(pointCounter > 11){
document.body.style.backgroundColor="blue";
}
}
</script>
</body>
</html>
So its pretty much basic but I want to implement a feature where the user has 2 minutes to answer the questions otherwise the quiz finishes. I was thinking I should use a setTimeOut function where I pass the 'gettingAnswers' function in the parameter and set it 0 ms so it executes straight away since I don't want any delays BUT I don't know how to finish the execution of the gettingAnswers function if 2 minutes have passed... I was thinking on clearTimeOut() maybe?
clearTimeout(timer) allows to cancel a function call which wasn't yet executed, timer being the result of a previous call to setTimeout, i.e. var timer = setTimeout(gettingAnswers, 0);. So this is not your case.
You should implement a function finishQuiz(); which you call with a setTimeout and gettingAnswers should be called without any delay:
gettingAnswers();//executed immediately and builds the questions on page
setTimeout(finishQuiz, 2 * 60 * 1000);//executed in 2 minutes and stops quiz
Where finishQuiz should calculate the score, display it and hide questions. While the user is taking time to answer you shouldn't be executing any functions in my vision.
You question is more about algorithm not about programming technique. Better site for it would be Code Review StackExchange
Related
I'm creating a communications generator that will standardise SMS communications regarding critical IT incidents for my company. I've got an IF/ELSE statement that identifies whether an issue is new, updated, or resolved to pull the necessary information together and format it accordingly. As far as I can tell, everything here is fine, however I'm having an issue writing to a text box ('smsToSend') which should allow the user to review before they copy the text across to our sms sender app, as nothing is being output into this box.
function generateSMS(){
var issueTitle = document.getElementById("incidentTitle");
var advisorImpact = document.getElementById("advisorImpact";
var incidentUpdate = document.getElementById("incidentUpdate");
var incidentStatus = document.getElementById("incidentState");
var startTime = document.getElementById("startTime");
var endTime = document.getElementById("endTime");
var incidentPriority = document.getElementById("incidentPriority");
var incidentBrand = "TechTeam";
var systemImpacted = document.getElementById("systemImpacted");
var incidentReference = document.getElementById("incidentReference");
var smsToSend = document.getElementById("smsToSend");
if (incidentStatus != "Closed"){
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT RESOLVED: " + systemImpacted + ": " + incidentUpdate + ": Start: " + startTime + " End: " + endTime + " Reference: " + incidentReference;
}
else{
if (incidentUpdate == "We are investigating this issue"){
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT ISSUE: " + systemImpacted + ": " + issueTitle + ". " + advisorImpact + ": " + incidentReference;
}
else {
smsToSend = "P" + incidentPriority + " " + incidentBrand + "IT UPDATE: " + systemImpacted + ": " + incidentUpdate + ": " + incidentReference;
}
}
}
generateSMS();
alert.getElementById(smsToSend);
Try replacing all assignments to smsToSend like this:
smsToSend = yourValue;
With this:
smsToSend.value = yourValue;
Your problem happens because smsToSend is an instance of an element, rather than a variable linked to the displayed text. To update the element's value, you have to change the value property of the element.
The Right way of setting the value to a text box is
var smsToSend = document.getElementById("smsToSend");
smsToSend.value = "Your value";
After loading this code into webView:
String fun = "javascript:function getTextPage(){" +
" if(document.caretRangeFromPoint){" +
" var caretRangeStart = document.caretRangeFromPoint(0, 0);\n" +
" var caretRangeEnd = document.caretRangeFromPoint(window.innerWidth, window.innerHeight);\n" +
" } else {\n" +
" return null;\n" +
" }" +
" var range = document.createRange();\n" +
" range.setStart(caretRangeStart.startContainer, caretRangeStart.startOffset);\n" +
" range.setEnd(caretRangeEnd.endContainer, caretRangeEnd.endOffset);\n" +
" alert(range.toString());" +
"};";
loadUrl(fun);
loadUrl("javascript:getTextPage()");
method findAll() or findAllAsync() find text but doesn't highlisht it
Here is my code :
//Player Loadout
//Version 1.0 by Madi Magdy
function Player(name,sex,side) {
this.name = name;
this.sex = sex;
this.side = side;
}
//var newPlayer = new Player(prompt("Please enter your name"),prompt("Please enter your sex"),prompt("Please enter your nationality"));
//console.log("Hello " + newPlayer.name + "!");
function Class(classType,mainWeapon,sideWeapon) {
this.classType = classType;
this.mainWeapon = mainWeapon;
this.sideWeapon = sideWeapon;
}
assaultClass = new Class("Assault","M416","M9");
engineerClass = new Class("Engineer","AK5C","P226");
reconClass = new Class("Recon","SRR61","Deagle44");
supportClass = new Class("Support","M249","M1911");
classChooser = function(playerChoice) {
if (playerChoice === assaultClass.classType) {
console.log("You chose the " + assaultClass.classType + " " + "class! Your default weapons are " + assaultClass.mainWeapon + " " + "and " + assaultClass.sideWeapon);
}
else if (playerChoice === engineerClass.classType) {
console.log("You chose the " + engineerClass.classType + " " + "class! Your default weapons are " + engineerClass.mainWeapon + " " + "and " + engineerClass.sideWeapon);
}
else if (playerChoice === reconClass.classType) {
console.log("You chose the " + reconClass.classType + " " + "class! Your default weapons are " + reconClass.mainWeapon + " " + "and " + reconClass.sideWeapon);
}
else if (playerChoice === supportClass.classType) {
console.log("You chose the " + supportClass.classType + " " + "class! Your default weapons are " + supportClass.mainWeapon + " " + "and " + supportClass.sideWeapon);
}
};
classChooser(prompt("Please choose your class! Type in Assault or Engineer or Recon or Support!"));
classWeapons = function(weapon,attach,camo) {
this.weapon = weapon;
this.attach = attach;
this.camo = camo;
};
yourClassWeapons = new classWeapons(prompt("Choose your weapon"),prompt("Choose your weapon attachment"),prompt("Choose your camouflage color"));
var classChosen = function(yourChosenClass) {
if (yourChosenClass === assaultClass.classType) {
console.log("Your weapons are listed as follows");
console.log(yourClassWeapons);
}
else {
console.log("NOPE");
}
};
classChosen(classChooser()); //I also tried classChosen(classChooser);
I enter the string "Assault" when prompted and when I try to pass the classChooser function as an argument in my classChosen function, the statement after the if never executes but the else does. What mistake do I have?
The main problem is that you are not passing to classChosen what you think you are passing. The way it is, the variable yourChosenClass is undefined, which is why it isn't equal to assaultClass.classType.
You need to either store the value that the player chose, or return it from classChooser. Also, it would be much better for your code if the prompt for classChooser was part of the function instead of being passed as a parameter. So it could be something like this:
classChooser = function() {
var playerChoice = prompt("Please choose your class! Type in Assault or Engineer or Recon or Support!");
if (playerChoice === assaultClass.classType) {
console.log("You chose the " + assaultClass.classType + " " + "class! Your default weapons are " + assaultClass.mainWeapon + " " + "and " + assaultClass.sideWeapon);
}
else if (playerChoice === engineerClass.classType) {
console.log("You chose the " + engineerClass.classType + " " + "class! Your default weapons are " + engineerClass.mainWeapon + " " + "and " + engineerClass.sideWeapon);
}
else if (playerChoice === reconClass.classType) {
console.log("You chose the " + reconClass.classType + " " + "class! Your default weapons are " + reconClass.mainWeapon + " " + "and " + reconClass.sideWeapon);
}
else if (playerChoice === supportClass.classType) {
console.log("You chose the " + supportClass.classType + " " + "class! Your default weapons are " + supportClass.mainWeapon + " " + "and " + supportClass.sideWeapon);
}
return playerChoice;
};
classChosen(classChooser());
// or...
// var playerChosenClass = classChooser();
// classChosen(playerChosenClass);
Additionally, choosing the weapon can be refactored in a similar way:
weaponChooser = function() {
var weapon = prompt("Choose your weapon");
var attachment = prompt("Choose your weapon attachment");
var camo = prompt("Choose your camouflage color");
return new classWeapons(weapon, attachment, camo);
}
yourClassWeapons = weaponChooser();
Your classChooser function returns undefined, so in
classChosen(classChooser());
You pass in an argument parameter as undefined. That is else statement gets executed.
To make this work, you need to
classChooser = function(playerChoice) {
var returnType;
if (playerChoice === assaultClass.classType) {
console.log("You chose the " + assaultClass.classType + " " + "class! Your default weapons are " + assaultClass.mainWeapon + " " + "and " + assaultClass.sideWeapon);
return assaultClass.classType;
}
else if (playerChoice === engineerClass.classType) {
console.log("You chose the " + engineerClass.classType + " " + "class! Your default weapons are " + engineerClass.mainWeapon + " " + "and " + engineerClass.sideWeapon);
returnType = engineerClass.classType;
}
else if (playerChoice === reconClass.classType) {
console.log("You chose the " + reconClass.classType + " " + "class! Your default weapons are " + reconClass.mainWeapon + " " + "and " + reconClass.sideWeapon);
returnType = reconClass.classType;
}
else if (playerChoice === supportClass.classType) {
console.log("You chose the " + supportClass.classType + " " + "class! Your default weapons are " + supportClass.mainWeapon + " " + "and " + supportClass.sideWeapon);
returnType = supportClass.classType;
}
return returnType;
};
classChoosen(classChooser(prompt("Please choose your class! Type in Assault or Engineer or Recon or Support!")));
Instead of looping through one time to show
id1
id2
id3
it loops through 3 times before stopping. what can i put to make it loop through only once.
html:
<p id="show_me"></p>
<button onclick="ObjectArray()">click me</button>
javascript:
var ObjectArray = function() {
// object literal
var id1 = {
firstName: "John",
lastName: "Doe",
id: "12345"
};
// keyword new
var id2 = new Object;
id2.firstName = "Adam";
id2.lastName = "Bakely";
id2.id = "abcdef";
// object constructor
function employee(first, last, id) {
this.firstName = first;
this.lastName = last;
this.id = id;
}
var id3 = new employee("Dallas", "Star", "abc123");
//create an array
var IdArray = [id1, id2, id3];
//for loop to display results
var text="";
var i;
for (i = 0; i < IdArray.length; i++){
text += IdArray[0].firstName + " " + IdArray[0].lastName + " " + IdArray[0].id + "<br>";
text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";
}
document.getElementById("show_me").innerHTML = text;
}
It iterates three times, because you loop for the length of the array, which has 3 items.
If you want to 'iterate' once, you can just omit the for loop:
text += IdArray[0].firstName + " " + IdArray[0].lastName + " " + IdArray[0].id + "<br>";
text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";
But I think you actually wanted to do this:
for (i = 0; i < IdArray.length; i++){
text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
}
That way you use the loop what it's for: Iterate over an array of any length and repeat a piece of code for each item in the array.
Remove everything from the for loop, and add this instead:
text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
Every thing is fine ... but please replaced the following code
for (i = 0; i < IdArray.length; i++){
text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
text += IdArray[1].firstName + " " + IdArray[1].lastName + " " + IdArray[1].id + "<br>";
text += IdArray[2].firstName + " " + IdArray[2].lastName + " " + IdArray[2].id + "<br>";
}
with
for (i = 0; i < IdArray.length; i++){
text += IdArray[i].firstName + " " + IdArray[i].lastName + " " + IdArray[i].id + "<br>";
}
I'm trying to perform a query in Android using: return query(selection, null, null, null), but cannot return the needed results when using multiple operators and parentheses. This is what I'm trying to do:
selection = "(KEY_VARIABLE > '5' AND KEY_VARIABLE2 = 'Yes') OR (KEY_VARIABLE < '5' AND KEY_VARIABLE2 = 'No')"
However, the query returns 0 results, because the query does not recognize the parentheses.
Is there a way to form a query in Android using multiple operators with an operator embedded within an operator statement?
UPDATE: There's no exception in logcat. Everything works fine, except for the embedded OR(AND) statements. Here's the code:
public Cursor getTierSchools() {
String range = "";
String range2 = "";
String range3 = "";
double score = LabValues.myscore;
double scoreplustwo = score + 2;
double scoreminustwo = score - 2;
double scoreplusfive = score + 5;
double scoreminusfive = score - 5;
double scoreminusone = score - 1;
range = "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplusfive + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " = " + "'" + LabValues.mystate + "'" + " )";
range2 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreminusone + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " != " + "'" + LabValues.mystate + "'" + " )";
range3 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplustwo + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'No'" + " )";
String selection = range + range2 + range3;
return query(selection, null, null, sorting);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns, String sortOrder) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS_VIRTUAL_TABLE);
builder.setProjectionMap(mColumnMap);
Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
columns, selection, selectionArgs, null, null, sortOrder);
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
Basically, I'm trying to integrate AND statements within parentheses, similar to how you would form an equation like: (4+4)/8 = 1 IS NOT EQUAL TO 4+4/8 = 4.5 .
SOLUTION:
range = "(( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplusfive + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " = " + "'" + LabValues.mystate + "'" + " )";
range2 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreminusone + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'Yes' AND " + KEY_SCHOOLSTATELONG + " != " + "'" + LabValues.mystate + "'" + " )";
range3 = " OR " + "( " + KEY_SCHOOLSCORE + " > " + "'" + scoreplustwo + "'" + " AND " + KEY_SCHOOLSTATEBIAS + " = 'No'" + " ))";