Checking if an item exists in Action Script - actionscript-3

Sorry, I've never done AS before, so I apologize for the basic question. There is a line in this file I am trying to modify:
var media:Namespace = rssXML.channel.item[i].namespace("media");
I'm just trying to check to see if it exists and if it has a value?
I know in PHP it would be
if(isset(rssXML.channel.item[i].namespace("media") && !empty(rssXML.channel.item[i].namespace("media")) {
//Do Something
}
What would be the AS equivalent?

if (variablename) { // it's there } else { // it's not }

All AS classes extend Object which has a hasOwnProperty() test which returns a boolean if a property with the name exists. Then you can test if (property) or if (property == null).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Object.html#hasOwnProperty()

Related

is there a name for this conditional-checking technique?

so, theres a technique I use in a fair amount of programming languages and projects; and I'm wondering if it has a general, language-agnostic "official" term to describe it.
basically, I nicknamed it "trip switch checking." Its where if you need to check that several variables have specific values, possibly of different types, you first set a boolean to "false" and then in either a loop or several if statements, you check what you need to by setting the boolean to true if any of the other variables don't meet your requirements.
I call it trip-switching because the boolean remains false if the "switch" isn't "tripped," using an analogy from a common safety mechanism on industrial machinery where if something moves too far or gets too close or etc. a physical switch is actually bumped into and it shuts the whole thing down. the idea is not to switch it back off until the obstruction is cleared- the machine cant turn itself back on automatically.
pseudocode example, a function that returns true if the trip switch wasn't hit:
function tripswitchcheck()
{
boolean tripswitch = false
if(idontnwantthis == true)
{
tripswitch = true
}
if(iwantthis == false)
{
tripswitch = true
}
//...etc...basically do stuff to check stuff. if any values are undesired, true the tripswitch.
return !tripswitch
}
It may be bad practice depending on the language and nature of the project, but it works, and that's outside of the scope of this question.
First of all, why would you not just have a positive flag in that example:
function tripswitchcheck()
{
boolean success = true;
if(idontnwantthis == true)
{
success = false;
}
if(iwantthis == false)
{
success = false;
}
//...etc...basically do stuff to check stuff. if any values are undesired, falsify success
return success
}
Secondly, why check the conditions if you already know you have failure. Say that we don't have a forward goto in the language, or else, or early returns (or we don't want to return because something else is done based on the success flag before returning):
function tripswitchcheck()
{
boolean success = true;
if(idontnwantthis == true)
{
success = false;
}
if(success && iwantthis == false) // if success is false, fall through
{
success = false;
}
if(success && !othercondition()) // likewise
{
success = false;
}
//...etc...
return success
}
There are reasons not to use else if some of the conditions make use of earlier results.
Anyway, this just comes from basic logic and scientific reasoning: we have a hypothesis and look for reasons why it is not true.
The name for it is perhaps "single exit rule" and such: the function exits through a single return statement.

Why is the "Add Note" button giving an exception and printing null on the console?

I am trying to add a note using Add Note Button but it is throwing an exception. I handled the error using try...catch block but the note written in Add Note Button is not added and printing null on console.
JSBin for my project:
This piece:
if(notes =='')
{
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
is the problem. If there's nothing in the local storage, notes will be null, which means notesObj == '' will be false, and the code will break. You need to change the test so it detects null (and other falsy values) as such:
if(!notes)
{
notesObj = [];
}
else{
notesObj = JSON.parse(notes);
}
There's another problem. This line:
localStorage.setItem('notes', JSON.stringify(notes));
makes no sense. You're setting the item notes in the local storage as the converted json of the notes variable, which is already a JSON string, so it'll fail horribly. What you really want to do is stringify the notesObj variable, which is the array with the actual notes. Change that line to:
localStorage.setItem('notes', JSON.stringify(notesObj));
And that's it. Make those changes and your code will work.

Cannot find method moneyToMicros((class))

I'm trying to programmatically change the max cpc with an AdWords script, but I'm getting an error. The console just says "Cannot find method moneyToMicros((class))". I cannot find any documentation about this error or any other posts about this anywhere. Was wondering if anyone knew how to get around this. Here's a small snippet of the code where the error occurs (error occurs on the line where setKeywordMaxCpc() is called):
while (adGroupIterator.hasNext())
{
var adGroup = adGroupIterator.next();
var adGroupName = adGroup.getName();
if (adGroupRegex.test(adGroupName))
{
if (adGroup.isPaused())
{
adGroup.enable();
adGroup.setKeywordMaxCpc(bidModifier);
}
}
else
{
adGroup.pause();
}
}
I had the same problem and I've just resolved it!
I was passing the value "null" to the function .setKeywordMaxCpc();
So I think you need to check if the variable bidModifier is null before you execute the function .setKeywordMaxCpc(bidModifier );
In my case I was using the value keyword.getFirstPageCpc() to set my bid, and in some words that value is null.

Windows Phone speech recognition - words that are not in grammar

consider a grammar like this ; speech.Recognizer.Grammars.AddGrammarFromList("answer",new string[] { "Go.","no" });
When I say something else that are not in grammar, she says "sorry didnt catch" and then tries to start it again. Same goes for null input.
What I want is that it should only recognize the words in grammar and for everything else it should just pass the recognition. I don't want to see anything like "sorry didnt catch" and second time recognotion. Any idea ? thanks.
Edit : with try-catch I can avoid from second time recognotion if the word is unknown but now it's waiting too long on "sorry didnt catch" part.
try
{
SpeechRecognizerUI speech = new SpeechRecognizerUI();
speech.Settings.ReadoutEnabled = false;
speech.Settings.ShowConfirmation = false;
speech.Recognizer.Settings.InitialSilenceTimeout = System.TimeSpan.FromSeconds(0.8);
speech.Recognizer.Grammars.AddGrammarFromList("answer", new string[] { "Go.", "no" });
SpeechRecognitionUIResult result = await speech.RecognizeWithUIAsync();
if (result.RecognitionResult.Text == "Go.") { .... }
}
catch
{
..... }
In my opinion, you must build your own UI to avoid this. So you should use SpeechRecognizer and then you can handle the input as you want.
In my case I even created two SpeechRecognizer, on with own Wordlist, the other one with default dictionary. It works like a charm, but I couldn't get it to work with SpeechRecognizerUI.

Checking whether a checkbox is checked

I have not found any inbuilt function for checking whether a checkbox is set true or false,
isChecked() is not available is apps-script (if i am right).
Any idea on how to find it or we shall have a value change handler to count the number of times the value changed and find if it is checked or not?
You should assign your checkbox a name so that you can retrieve its value in a handler function with e.parameter.checkboxname : this value is boolean.
var chkmode = app.createCheckBox("description").setName("chk1").setId("chk1")
with the ID you can modify its state from the handler function (or from any other) if necessary (getElementbyId())
note that the handler can be on the checkbox itself (a change handler) or on any other element in the UI, depending on your needs.
I might be wrong, however I believe there is a problem with the checkBox status, even though its status looks like boolean, it doesn't behave like boolean but like string
if you print in a spreadsheet e.parameter.myCheckBox you will get TRUE or FALSE
if you print in a spreadsheet e.parameter you will get the whole object and see myCheckBox=true or myCheckBox=false
however, if (e.parameter.myCheckBox) will always return True
the workaround I am using is: if (e.parameter.myCheckBox == "true") will return the actual myCheckBox status
Just in case, I am opening a new ticket in the issue tracker
I hope this would help you
ps. confirmed with Google, e.parameter.myCheckBox is a string, not a boolean
if you want to use it as boolean it would be like (e.parameter.myCheckBox == "true")
var handler = app.createServerClickHandler('tellStatus');
and then (as example showing it in a label)
function tellStatus(e){
var app = UiApp.getActiveApplication();
app.getElementById('yourStatusLabel').setText('Checkbox checked: ' + e.parameter.yourCheckbox)
return app;
}
You should use handler function look like below.
I found example from https://sites.google.com/site/scriptsexamples/learn-by-example/uiapp-examples-code-snippets/check-box
if(e.parameter['checkbox_isChecked_'] == 'true'){
itemsSelected+= e.parameter['checkbox_value_']+',';
}