STRING COMPARSION FLASH AS3 - actionscript-3

I got an input textfield in the UI.
When user key in "GIRAFFEEE", "GIRAFEAAA" OR "GIRAFFE123" and submit. The score value should be 0. However it returns 1.
How do I compare case sensitive string correctly?
qns1 = qns1_txt.text.toLowerCase();
qns1Ans = "giraffe"
//.toLowerCase();
if (qns1 == qns1Ans)
{
score = 1;
}
else
{
score = 0;
}

If you test following:
var correct:String = "giraffe";
var userAns:String = "giraffeaaaa";
trace(correct == userAns);//false - as expected
it means that string comparison works:)
I assume that your test code is in the CHANGE event of the textfield which may result in false positive as user may type part of correct answer, I think you should do a function:
function validate()
{
qns1 = qns1_txt.text.toLowerCase();
qns1Ans = "giraffe"
score = 0;
if(qns1 == qns1Ans)
{
score = 1;
}
}
and call it when user will hit submit, you could also compare the length of strings but equal operator will do just fine.

Related

Passing variable to htp.p in a PL/SQL Dynamic Content

I am trying to pass a value in a variable for validation based on a db column and a checkbox on the UI component.
NOTE: Max_Set is the variable holding the maximum number from the DB column which is to be validated against the numberOfChecked items. However, when i pass Max_Set to the htp.p script, i am unable to use the value in the variable.
select num into Max_Set from table where column_name = name;
htp.p('<script type="text/javascript">');
htp.p('function ValidateSelection1()
{
var checkboxes = document.getElementsByName("student");
var numberOfCheckedItems = 0;
var Max_num = Max_Set
for(var i = 0; i < checkboxes.length; i++)
{
if(checkboxes[i].checked)
numberOfCheckedItems++;
}
if(numberOfCheckedItems > Max_num)
{
alert("You cant select more than the required number for this SL Site!");
return false;
}
}');
htp.p('</script>');
How can I use the value from Max_Set into htp.p script without it being considered as a string?
In your pl/sql code, everything within the htp call is treated as a string. If you want to use variables that are declared in pl/sql, you can concatenate (||) to put it all together. I replaced the "Max_Set" with "l_max_set" to indicate that this is a local pl/sql variable and not a javascript variable and the initcap tends to make it look like javascript.
select num into l_max_set from table where column_name = name;
htp.p('<script type="text/javascript">');
htp.p('function ValidateSelection1()
{
var checkboxes = document.getElementsByName("student");
var numberOfCheckedItems = 0;
var Max_num = '||l_max_set||';
for(var i = 0; i < checkboxes.length; i++)
{
if(checkboxes[i].checked)
numberOfCheckedItems++;
}
if(numberOfCheckedItems > Max_num)
{
alert("You cant select more than the required number for this SL Site!");
return false;
}
}');
htp.p('</script>');

Guessing Number Game Program not Functioning Correctly

in my program, the user sets a range of numbers for the computer to guess. The user then has to guess which number the computer chose with a limit of guesses starting at 5. There are several problems in my functioning program in which I do not understand how to fix. These errors include:
-The number of guesses left always remains at 0. It won't start at 5 and decrease by 1 each time I click the btnCheck button.
-Whenever I click the btnCheck button for a new guessing number, the statement if you've guessed too high or too low remains the same.
-When I press btnNewGame, the values I insert in my low value and my high value text inputs will not be cleared.
-How can the computer generate a random whole number based on what I set as the number range?
Revising my code down below will be much appreciated.
// This line makes the button, btnCheckGuess wait for a mouse click
// When the button is clicked, the checkGuess function is called
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
// This line makes the button, btnNewGame wait for a mouse click
// When the button is clicked, the newGame function is called
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Declare Global Variables
var computerGuess:String; // the computer's guess
var Statement:String; // Statement based on your outcome
// This is the checkGuess function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function checkGuess(e:MouseEvent):void
{
var LowValue:Number; // the user's low value
var HighValue:Number; // the user's high value
var UserGuess:Number; // the user's guess
var CorrectGuess:int; // the correct number
var FirstGuess:String; //the user's guess
// get the user's range and guess
LowValue = Number(txtinLow.text);
HighValue = Number(txtinHigh.text);
UserGuess = Number(txtinGuess.text);
// determine the number of the user
GuessesLeft = checkCorrectGuess(FirstGuess);
lblNumber.text = GuessesLeft.toString();
lblStatement.text = "You have guessed " + Statement.toString() + "\r";
}
// This is function checkColoursCorrect
// g1– the user's guess
function checkCorrectGuess(g1:String):int
{
var GuessesLeft:int = 5; // How many guesses are left
if (g1 != computerGuess)
{
GuessesLeft - 1;
}
else
{
GuessesLeft = 0;
}
return GuessesLeft;
}
// This is the newGame function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function newGame(e:MouseEvent):void
{
var Guess1:int; // computer's guess in numbers
var UserGuess1:int; // user's guess in numbers
Guess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
UserGuess1 = randomWholeNumber(100,1); //It is not (100,1). How do I change this to the range the user put?
if (Guess1 > UserGuess1) {
Statement = "TOO HIGH";
} else if (Guess1 < UserGuess1) {
Statement = "TOO LOW";
} else if (Guess1 == UserGuess1) {
Statement = "CORRECTLY";
}
txtinGuess.text = "";
lblStatement.text = "";
}
// This is function randomWholeNumber
// highNumber – the maximum value desired
// lowNumber – the minimum value desired
// returns – a random whole number from highNumber to lowNumber inclusive
function randomWholeNumber(highNumber:int,lowNumber:int):int //How do I make a whole random number based on the range the user made?
{
return Math.floor((highNumber - lowNumber + 1) * Math.random() + lowNumber);
}
To answer your questions...
You've declared GuessesLeft inside checkCorrectGuess() which means its a local variable that's being redefined every time you call the function. Futhermore, because you're passing in var FirstGuess:String; (an uninitialized, non-referenced string variable), (g1 != computerGuess) is returning false, and the answer is always 0.
GuessesLeft - 1; is not saving the result back to the variable. You need to use an assignment operator such as GuessesLeft = GuessesLeft - 1 or simply type GuessesLeft-- if all you want is to decrement. You could also write GuessesLeft -= 1 which subtracts the right from the left, and assigns the value to the variable on the left. See AS3 Operators...
You've already assigned values to these TextFields earlier; simply repeat the process inside of newGame() with a txtinLow.text = "" (same with high)
Use your variables. You defined them earlier in checkGuess() as UserGuess, LowValue, and HighValue
Be mindful that you only need to split out functionality into separate functions if that piece of code is likely to be called elsewhere. Otherwise, every function on the stack incurs more memory and performance hits. checkCorrectGuess() falls into that category and is therefore unnecessary.
Also, you are printing your feedback to the user in the newGame() function instead of checkGuess(). It seemed like an oversight.
btnCheckGuess.addEventListener(MouseEvent.CLICK, checkGuess);
btnNewGame.addEventListener(MouseEvent.CLICK, newGame);
// Global Variables
var computerGuess:int;
var remainingGuesses:int;
newGame();
function newGame(e:MouseEvent):void {
// Reset our guess limit
remainingGuesses = 5;
// Generate a new number
computerGuess = random(int(txtinLow.text), int(txtinHigh.text));
// Reset our readouts.
txtinGuess.text = "";
lblStatement.text = "";
}
function checkGuess(e:MouseEvent):void {
var guess:int = int(txtinGuess.text);
var msg:String;
if (guess == computerGuess) { // Win
remainingGuesses = 0; // Zero our count
msg = "CORRECT";
} else { // Missed
remainingGuesses--; // Decrement our count
if (guess > computerGuess) {
msg = "TOO HIGH";
} else if (guess < computerGuess) {
msg = "TOO LOW";
}
}
lblNumber.text = remainingGuesses.toString();
lblStatement.text = "You have guessed " + msg;
}
function random(low:int, high:int):int {
return Math.floor((high - low + 1) * Math.random() + low);
}

1170: Function does not return a value

I am a bit new to flash and actionscript but i am learning. With this code I am trying to get a message that says true when the value is between from and to. When I run this it gives the error in the title. What am I doing wrong?
from = Number(txtFra.text);
value = Number(txtTall.text);
to = Number(txtTil.text);
var from:Number;
var value:Number;
var value:Number;
function insideIntervall(from:int, value:int, to:int):Boolean
{
var bool:Boolean
if (from<value<to)
{
bool = true;
}
else
{
bool = false;
}
if (bool == true)
{
trace("True");
}
else
{
trace("False");
}
}
The error in the title is because your function must explicitly return a value. You do this with the keyword return.
However, there is another error in your program: you cannot compare from<value<to. What you need to check is that from < value && value < to. Basically, that both conditions are true.
The body of your function could then be simplified to: return from < value && value < to;

Getting NaN(Not a Number) in Adobe Flash

I wanted to make a little gas calculator in Flash with AS but i am getting the error "NaN" in my textfield even BEFORE i enter anything inside the textfield. Any ideas where the problem is? Many thanks in advance. Here is my actionscript code:
km_txt.restrict = ".0-9";
liter_txt.restrict = ".0-9";
priceliter_txt.restrict = ".0-9";
stage.addEventListener(Event.ENTER_FRAME, calculate);
function calculate(param1:Event)
{
if (liter_txt.text != "" && km_txt.text != "")
{
usage_txt.text = String(100 * Number(liter_txt.text) / Number(km_txt.text));
}
if (liter_txt.text != "" && km_txt.text != "" && priceliter_txt.text != "")
{
cost_txt.text = String(Number(liter_txt.text) / Number(km_txt.text) * Number(priceliter_txt.text));
}
if (liter_txt.text != "" && priceliter_txt.text != "")
{
total_txt.text = String(Number(liter_txt.text) * Number(priceliter_txt.text));
}
}
You are casting to Number a few times from TextField objects but those at that point don't contain anything so the cast resolve to NaN:
String(100 * Number(liter_txt.text) / Number(km_txt.text));
Now trying to add/multiply/divide Number and NaN together still resolve to NaN.
You need to check for value first and maybe set to 0 if you get NaN, store in variables to make things easier:
var value:Number = Number(liter_txt.text);
if(isNaN(value))
{
//this is not a number so substitute with 0?
value = 0;
}
Be sure about initial text values of your input TextFields since you are checking value of "". At the top of your block, write;
km_txt.text = "";
liter_txt.text = "";
priceliter_txt.text = "";
And it will be a better code if you listen to TextEvent.TEXT_INPUT events on TextFields and make calculations there.

ScriptDB object size calculation

I'm trying to estimate the limits of my current GAS project. I use ScriptDB to chunk out processing to get around the 6 min execution limit. If I have an object like
var userObj{
id: //user email address
count: //integer 1-1000
trigger: //trigger ID
label: //string ~30 char or less
folder: //Google Drive folder ID
sendto: //'true' or 'false'
shareto: //'true' or 'false'
}
How would I calculate the size that this object takes up in the DB? I would like to project how many of these objects can exist concurrently before I reach the 200MB limit for our domain.
Whenever you've got a question about google-apps-script that isn't about the API, try searching for javascript questions first. In this case, I found JavaScript object size, and tried out the accepted answer in apps-script. (Actually, the "improved" accepted answer.) I've made no changes at all, but have reproduced it here with a test function so you can just cut & paste to try it out.
Here's what I got with the test stud object, in the debugger.
Now, it's not perfect - for instance, it doesn't factor in the size of the keys you'll use in ScriptDB. Another answer took a stab at that. But since your object contains some potentially huge values, such as an email address which can be 256 characters long, the key lengths may be of little concern.
// https://stackoverflow.com/questions/1248302/javascript-object-size/11900218#11900218
function roughSizeOfObject( object ) {
var objectList = [];
var stack = [ object ];
var bytes = 0;
while ( stack.length ) {
var value = stack.pop();
if ( typeof value === 'boolean' ) {
bytes += 4;
}
else if ( typeof value === 'string' ) {
bytes += value.length * 2;
}
else if ( typeof value === 'number' ) {
bytes += 8;
}
else if
(
typeof value === 'object'
&& objectList.indexOf( value ) === -1
)
{
objectList.push( value );
for( i in value ) {
stack.push( value[ i ] );
}
}
}
return bytes;
}
function Marks()
{
this.maxMarks = 100;
}
function Student()
{
this.firstName = "firstName";
this.lastName = "lastName";
this.marks = new Marks();
}
function test () {
var stud = new Student();
var studSize = roughSizeOfObject(stud);
debugger;
}