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!")));
Related
Description: A complex structure of HTML elements (rectangles) displayed on the screen, no overlaps, each rectangle having a distinct HTML id attribute (hence pickable by Selenium IWebDriver and C# code).
Target: I need programatically with Selenium and C# to create and trigger mousewheel event (via IJavaScriptExecutor or some other methods) on a selected rectangle element.
Q: How this can be done? Thank you
This is my implementation after investigating
//wheelTicks: negative for zoomin, positive to zoomout
public object zoomElementById(string elemId, int wheelTicks=1)
{
object response = null;
string myJavaScript =
// Callback (place in first!) used to notify the caller that the async callee is ready
" var callback = arguments[arguments.length - 1]; " +
" var maxIntervals = arguments[1]; " +
//ms
" var intervalDuration = 150; " +
" console.log('javascripting...', callback, arguments); " +
"var d = new Date(); " +
"var n = d.getTime(); " +
" var myZoomCenterElement = document.getElementById('" + elemId + "'); " +
// some debug output in the console
" console.log(myZoomCenterElement); " +
// *** THE CORE OF THE SOLUTION *** Creates proper WheelEvent object and triggers WheelEvent(Zoom)
" var box = myZoomCenterElement.getBoundingClientRect(); " +
" var boxMiddleX = Math.round((box.right + box.left )/2); " +
" var boxMiddleY = Math.round((box.bottom + box.top )/2); " +
" var myWheelableElement = document.getElementsByClassName('svg-tree-view')[0]; " +
" var wheelEventInitDict = { " +
" 'deltaX' : 0.0, " +
" 'deltaY' : -200.0, " +
" 'deltaZ' : 0.0, " +
" 'deltaMode' : 0, " +
" 'clientX' : boxMiddleX, " +
" 'clientY' : boxMiddleY " +
" }; " +
" var myWheelEvent = new WheelEvent('wheel', wheelEventInitDict); " +
" console.log(wheelEventInitDict, boxMiddleX, boxMiddleY, myWheelEvent); " +
" var myIntervalCounter = 0; " +
" var myInterval = setInterval(function(){ " +
" myWheelableElement.dispatchEvent(myWheelEvent); " +
" myIntervalCounter++; " +
" if (myIntervalCounter > maxIntervals) clearInterval(myInterval); " +
" }, intervalDuration); " +
" var sthToReturn = 'Returning: Nothing requested!'; " +
" var asyncAwaitInMiliSeconds = Math.ceil( 1.2 * intervalDuration * maxIntervals ); " +
// Triggers the callback (to indicate async ready)
" setTimeout( function(){ " +
" console.log((new Date()).getTime()-n); " +
" callback(sthToReturn); " +
" }, asyncAwaitInMiliSeconds); " +
""
;
_driver.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, 0, 20));
IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
try
{
// addititonal args(optional) to be sent to the javascript func are put after the first arg
return response = js.ExecuteAsyncScript(myJavaScript, elemId, wheelTicks);
}
catch(UnhandledAlertException e)
{
Console.WriteLine("Error Occured! \n {0}", e.ToString() );
return null;
}
}
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
I'm trying to create a little text based rpg to put in practice what i learned of javascript. I tried two approaches, one is working and the other one isn't it, and I want to know why.
In the first one, I created an object "monster" with a method that sets all the properties of each monster (hit points, attack points, etc). The problem with this it's i have to write the properties one by one, and it makes the code too long for my taste.
/* Write JavaScript here */
/* Write JavaScript here */
$(document).ready(function() {
var hero = {
hitPoints: 13,
armorClass: 10,
attackBonus: 1,
weaponDamage: 7,
heroStats: function() {
return "Hero" + "<br/>" +
"Hit Points: " + this.hitPoints + "<br/>" +
"Armor Class: " + this.armorClass + "<br/>" +
"Attack Bonus: " + this.attackBonus + "<br/>" +
"Weapon Damage: " + this.weaponDamage;
},
alive: true
};
var monster = {
name: "",
hitPoints: 0,
armorClass: 0,
attackBonus: 0,
weaponDamage: 0,
monsterStats: function() {
return this.name + "<br/>" +
"Hit Points: " + this.hitPoints + "<br/>" +
"Armor Class: " + this.armorClass + "<br/>" +
"Attack Bonus: " + this.attackBonus + "<br/>" +
"Weapon Damage: " + this.weaponDamage;
},
monsterSelected: false,
selectMonster: function() {
if (this.monsterSelected === false) {
switch (Math.floor(Math.random() * 2) + 1) {
case 1:
this.name = "Werewolf";
this.hitPoints = 20;
this.armorClass = 8;
this.attackBonus = 4;
this.weaponDamage = 3;
this.monsterSelected = true;
break;
case 2:
this.name = "Goblin";
this.hitPoints = 15;
this.armorClass = 10;
this.attackBonus = 4;
this.weaponDamage = 3;
this.monsterSelected = true;
break;
}
}
},
alive: true
};
monster.selectMonster();
$(".hero_info").html(hero.heroStats());
$(".monster_info").html(monster.monsterStats());
$("button").click(function() {
battle(monster);
});
function battle(actualmonster) {
if (actualmonster.alive === false) {
actualmonster.monsterSelected = false;
actualmonster.selectMonster();
$(".monster_info").html(actualmonster.monsterStats());
$(".battle_info").html("");
actualmonster.alive = true;
} else {
var d20 = Math.floor(Math.random() * 20) + 1;
var d_wp = Math.floor(Math.random() * hero.weaponDamage) + 1;
if (d20 + hero.attackBonus > actualmonster.armorClass) {
$(".battle_info").html("You attack!: d20+" + hero.attackBonus + ": " + (d20 + hero.attackBonus) + " vs AC " + actualmonster.armorClass + "<br/>" + "You hit!: d" + hero.weaponDamage + ": " + d_wp);
actualmonster.hitPoints = actualmonster.hitPoints - d_wp;
$(".monster_info").html(actualmonster.monsterStats());
} else {
$(".battle_info").html("You miss!: d20+" + hero.attackBonus + ": " + (d20 + hero.attackBonus) + " vs AC " + actualmonster.armorClass);
}
if (actualmonster.hitPoints <= 0) {
actualmonster.hitPoints = 0;
$(".monster_info").html(actualmonster.monsterStats());
$(".battle_info").html("You killed the monster!");
actualmonster.alive = false;
} else {
var d20_m = Math.floor(Math.random() * 20) + 1;
var d_wp_m = Math.floor(Math.random() * monster.weaponDamage) + 1;
if (d20_m + actualmonster.attackBonus > hero.armorClass) {
$(".battle_info").append("<br/>Monster attacks you!: d20+" + actualmonster.attackBonus + ": " + (d20_m + actualmonster.attackBonus) + " vs AC " + hero.armorClass + "<br/>" + "Monster hits you!: d" + actualmonster.weaponDamage + ": " + d_wp_m);
hero.hitPoints = hero.hitPoints - d_wp_m;
$(".hero_info").html(hero.heroStats());
} else {
$(".battle_info").append("<br/>Monster miss!: d20+" + hero.attackBonus + ": " + (d20_m + monster.attackBonus) + " vs AC " + hero.armorClass);
}
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<p class="monster_info"></p>
<p class="battle_info"></p>
<p class="hero_info"></p>
<button type="button">Attack</button>
</body>
In the second one, instead of creating and object "monster", i make a constructor, so i could define the monsters in a shorter and simplier way (for example: var phantom = new monster (prop1, prop2, prop3...)). The issue comes when i tried select a random monster for each battle. I tried a lot of things, but the problem remains the same: if a create a random function to select the monsters, this works every time a click the attack button, making a mess. This is driven me crazy, and i have no idea what to do. Any suggestions? Thanks in advance.
$(document).ready(function() {
var hero = {
hitPoints: 13,
armorClass: 10,
attackBonus: 1,
weaponDamage: 7,
heroStats: function() {
return "Hero" + "<br/>" +
"Hit Points: " + this.hitPoints + "<br/>" +
"Armor Class: " + this.armorClass + "<br/>" +
"Attack Bonus: " + this.attackBonus + "<br/>" +
"Weapon Damage: " + this.weaponDamage;
},
alive: true
};
function monster(name, hitpoints, armorclass, attackbonus, weapondamage) {
this.name = name;
this.hitPoints = hitpoints;
this.armorClass = armorclass;
this.attackBonus = attackbonus;
this.weaponDamage = weapondamage;
this.monsterStats = function() {
return this.name + "<br/>" +
"Hit Points: " + this.hitPoints + "<br/>" +
"Armor Class: " + this.armorClass + "<br/>" +
"Attack Bonus: " + this.attackBonus + "<br/>" +
"Weapon Damage: " + this.weaponDamage;
},
this.selected = false;
this.alive = true;
}
function selectMonster() {
var werewolf = new monster("Werewolf", 5, 4, 4, 3);
var goblin = new monster("Goblin", 15, 4, 4, 3);
switch (Math.floor(Math.random() * 2) + 1) {
case 1:
if (werewolf.selected === false) {
werewolf.selected = false;
return werewolf;
}
break;
case 2:
if (goblin.selected === false) {
goblin.selected = false;
return goblin;
}
break;
}
}
$(".hero_info").html(hero.heroStats());
$(".monster_info").html(selectMonster().monsterStats());
$("button").click(function() {
battle(selectMonster());
});
function battle(actualmonster) {
if (actualmonster.alive === false) {
actualmonster.selected = false;
actualmonster.selectMonster();
$(".monster_info").html(actualmonster.monsterStats());
$(".battle_info").html("");
actualmonster.alive = true;
} else {
var d20 = Math.floor(Math.random() * 20) + 1;
var d_wp = Math.floor(Math.random() * hero.weaponDamage) + 1;
if (d20 + hero.attackBonus > actualmonster.armorClass) {
$(".battle_info").html("You attack!: d20+" + hero.attackBonus + ": " + (d20 + hero.attackBonus) + " vs AC " + actualmonster.armorClass + "<br/>" + "You hit!: d" + hero.weaponDamage + ": " + d_wp);
actualmonster.hitPoints = actualmonster.hitPoints - d_wp;
$(".monster_info").html(actualmonster.monsterStats());
} else {
$(".battle_info").html("You miss!: d20+" + hero.attackBonus + ": " + (d20 + hero.attackBonus) + " vs AC " + actualmonster.armorClass);
}
if (actualmonster.hitPoints <= 0) {
actualmonster.hitPoints = 0;
$(".monster_info").html(actualmonster.monsterStats());
$(".battle_info").html("You killed the monster!");
actualmonster.alive = false;
} else {
var d20_m = Math.floor(Math.random() * 20) + 1;
var d_wp_m = Math.floor(Math.random() * hero.weaponDamage) + 1;
if (d20_m + actualmonster.attackBonus > hero.armorClass) {
$(".battle_info").append("<br/>Monster attacks you!: d20+" + actualmonster.attackBonus + ": " + (d20_m + actualmonster.attackBonus) + " vs AC " + hero.armorClass + "<br/>" + "Monster hits you!: d" + actualmonster.weaponDamage + ": " + d_wp_m);
hero.hitPoints = hero.hitPoints - d_wp_m;
$(".hero_info").html(hero.heroStats());
} else {
$(".battle_info").append("<br/>Monster miss!: d20+" + hero.attackBonus + ": " + (d20_m + actualmonster.attackBonus) + " vs AC " + hero.armorClass);
}
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="monster_info"></p>
<p class="battle_info"></p>
<p class="hero_info"></p>
<button type="button">Attack</button>
The second approach is good but you're using it weird. selectMonster will randomly generate monster with some random logic and a call to new monster. But then you call selectMonster() a few times for no apparent reason.
Your code should look like:
var current_monster = selectMonster(); // note that you already have function named monster
$(".monster_info").html(current_monster.monsterStats());
$("button").click(function() {
battle(current_monster);
});
You can also go all the way with prototyping to define your monsters:
function Monster(name, hp, attack) {
this.name = name;
this.hp = hp;
this.attack = attack;
this.alive = true;
// etc
}
Monster.prototype.monsterStats = function() {
return this.name + "<br/>" +
"Hit Points: " + this.hitPoints + "<br/>" +
"Armor Class: " + this.armorClass + "<br/>" +
"Attack Bonus: " + this.attackBonus + "<br/>" +
"Weapon Damage: " + this.weaponDamage;
}
function Werewolf() {
Monster.call(this, "werewolf", 5, 4); // call parent
}
Werewolf.prototype = new Monster();
function Goblin() {
Monster.call(this, "goblin", 15, 3); // call parent
}
Goblin.prototype = new Monster();
var goblin = new Goblin();
console.log(goblin.hp); // 15
console.log(goblin.monsterStats()); // goblin <br /> etc..
In your monster constructor, you're doing stuff in your return statement that you probably don't want to be. When you have something like return x + 1, foo = 3, bar = false the expressions are evaluated left to right and the last one is returned. That is, you're retuning the state of this.alive instead of returning the this expected in the constructor.
Try refactoring your code to explicitly return the monster object you want to be created by the constructor, and see if you still have problems there. Generally, you want your return to stand on its own, to avoid confusion around syntax.
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
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'" + " ))";