Ambiguous use of operator '!='? - function

I am trying to create an if else statement. If the randomNumber equals the text of a label, then I want to add 1 to the CorrectLabel. If they do not equal each other than I want to add 1 to the IncorrectLabel. Here is my code:
#IBAction func checkButton(sender: UIButton) {
if ( "\(randomImageGeneratorNumber)" == "\(currentCountLabel.text)"){
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
}else if ("\(randomImageGeneratorNumber)" != "\(currentCountLabel.text)"){
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}
}
I am getting an error on the "else if" statement line saying "Ambiguous use of operator '!=' ". I am unsure of what this error means or how to fix it.
What does this error mean and how can it be fixed?

You shouldn't compare like that. Just use .toInt() to cast the labeltext to int and compare it like that:
var currentCount = currentCountLabel.text?.toInt()
if randomImageGeneratorNumber == currentCount {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}
There is no need to put your value into a "".

First of all, you don't need to make comparison twice. Your code looks like
if true {
...
} else if false {
...
}
And, yes, int comparison would be better:
if let textAmount = currentCountLabel.text where randomImageGeneratorNumber == textAmount.toInt() {
currectAmountCorrect += 1
CorrectLabel.text = "\(currectAmountCorrect)"
} else {
currentAmountIncorrect += 1
IncorrectLabel.text = "\(currentAmountIncorrect)"
}

Related

html to plaintext with NodeJS on server side

on the server-side, using Nodejs. I receive a text message containing HTML. I want a function that converts the html to plain text. And please don't tell me to add the tag <plaintext> or <pre>. (convert_to_html function doesn't exist in nodejs)
socket.on('echo', (text) => {
plaintext = convert_to_html(text);
socket.emit('echo', {
message: plaintext
});
});
ideal results:
input: <h1>haha i am big</h1>
plaintext(what i want plaintext to be): <h1 &60;haha i am big </h1 &60;
output: <h1>haha i am big</h1>
current result:
input: <h1>haha i am big</h1>
plaintext: <h1>haha i am big</h1>
output: haha i am big
You can use the insertAdjacementHTML method on the browser side, here you go an example
socket.on("response", function (msg) {
const messages = document.getElementById("messages");
messages.insertAdjacentHTML("beforebegin", msg);
window.scrollTo(0, document.body.scrollHeight);
});
still don't have a proper solution. while i wait for one, i will use reserved characters as a temporary solution.
https://devpractical.com/display-html-tags-as-plain-text/#:~:text=You%20can%20show%20HTML%20tags,the%20reader%20on%20the%20browser.
function parse_to_plain_text(html){
var result = "";
for (var i = 0; i < html.length; i++) {
var current_char = html[i];
if (current_char == ' '){
result += " "
}
else if (current_char == '<'){
result += "<"
}
else if (current_char == '>'){
result += ">"
}
else if (current_char == '&'){
result += "&"
}
else if (current_char == '"'){
result += """
}
else if (current_char == "'"){
result += "&apos;"
}
else{
result += current_char;
}
}
return result;
}

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.

AS3 many buttons with boolean function - less verbose?

I have twenty eight instances of a two-frame MovieClip (frame1 = off - frame 2 = on) to select PDFs to send. The following code works fine, but I am looking to tighten it up and make it less verbose and easier to read. I include only one reference to an instance for space and sanity sake.
function PDFClick(e:MouseEvent):void {
targetPDF = e.target.ID;
trace("targetPDF " +targetPDF);
if (targetPDF == "PDF1")
if (pdf.pcconnectionPDF1.currentFrame == 1)
{
pdf.pcconnectionPDF1.gotoAndPlay(2);
PDF1 = 1;
trace("PDF1 is "+PDF1);
}else{
pdf.pcconnectionPDF1.gotoAndPlay(1);
PDF1 = 0;
trace("PDF1 is "+PDF1);
}
Thanks! trying to learn
You'll want to generalize your calls to your ID, that way you don't need special code for each condition.
function PDFClick(e:MouseEvent):void {
var ID:String = e.target.ID;
var mc = pdf["pcconnection" + ID];
if (mc.currentframe == 1) {
mc.gotoAndPlay(2);
this[ID] = 1;
} else {
mc.gotoAndPlay(1);
this[ID] = 0;
}
}
How about this:
function PDFClick(e:MouseEvent):void {
targetPDF = e.target.ID;
trace("targetPDF " +targetPDF);
if (targetPDF == "PDF1") {
var frame:int = pdf.pconnectionPDF1.currentFrame;
pdf.pconnectionPDF1.gotoAndPlay( frame == 1 ? (PDF1 = 1)+1 : (PDF1 = 0)+1 );
}
}
I think that's about what you are looking for.

ActionScript String Concatenation in for loop

So I am struggling with loops.
I have a mobile app with dynamic controls that I add from a sqlite database, it is a list of questions and based on the question type, I add the relevant type of control to the page along with the question, this all works fine.
I then loop through all the controls looking for answers, so I can loop through 60 quesitons and return the values from the relevant textboxes, checkboxes and toggle switches.
The for loop runs like this
if (displayObject is DisplayObjectContainer && currentDepth < maxDepth)
{
for (var i:int = 0; i < DisplayObjectContainer(displayObject).numChildren; i++)
{
traceDisplayList(DisplayObjectContainer(displayObject).getChildAt(i), maxDepth, skipClass, levelSpace + " ", currentDepth + 1);
if (displayObject is TextInput ||displayObject is CheckBox || displayObject is Label || displayObject is ToggleSwitch )
{
if(displayObject["id"] =="QuestionText"&& (i==0))
{
if(displayObject["text"] != null)
{
questionString= (displayObject["text"]);
trace ("Question: " + questionString);
}
}
else if (displayObject["id"] == "QuestionResponse")
{
if(displayObject["text"] != null)
{
answerString = (displayObject["text"]);
trace ("Answer: " + answerString);
}
}
else if (displayObject["id"]== "CheckboxResult")
{
if(displayObject["selected"] != null)
{
checkboxAnswer = (displayObject["selected"]);
trace ("Check / Toggle: " + checkboxAnswer);
}
}
}
}
}
My question is, the results I get back look like this;
questionstring value
answerstring value
checkbox value
what I want is
questionstring value, answerstring value, checkbox value
I cannot for the life of me see how I get these values into 1 row.
Any tips appreciated
Trace just prints info to the console mainly for debugging if you really want all of that on one line in the console you just need to create a string and then add "Question: " + questionString + " Answer: " + answerString + " Check / Toggle: " + checkboxAnswer to the string after you have found them all and then call trace with your string you made. something like this
var mystring:String
mystring="";
if(displayObject["id"] =="QuestionText"&& (i==0))
{
if(displayObject["text"] != null)
{
questionString= (displayObject["text"]);
mystring += "Question: " + questionString;
}
}
else if (displayObject["id"] == "QuestionResponse")
{
if(displayObject["text"] != null)
{
answerString = (displayObject["text"]);
mystring += "Answer: " + answerString;
}
}
else if (displayObject["id"]== "CheckboxResult")
{
if(displayObject["selected"] != null)
{
checkboxAnswer = (displayObject["selected"]);
mystring += "Check / Toggle: " + checkboxAnswer;
}
}
trace(mystring);