Having problems with Javascript Else If statement in Node Red - javascript

I have been fighting this problem all day. Heres a snippet of my code, 5 lines down in the else if is where things get screwy. I am also new to Javascript so that may be the reason I am unable to spot the mistake, but from what I have seen elsewhere, this code should work. Also the comments on lines 5 and 6 are swapped.
if (msg.payload.License_Plate !== null) {
// This portion checks for a valid number plate
if (readlpr == dblpr); { // we have a direct match, open the gate
opengate = 1; // will send open signal to gpio
} else if(readlpr !== null); { // from here on, we are checking for a partial plate match
validdigits = 0; // make sure we have data before continuing, as may be a rfid match
{
if (!context.count); { // check to see if counter already used, if not initialise it
context.count = 0;
Image of error message

You have a few errors:
if (readlpr == dblpr); {
...
} else if(readlpr !== null); {
...
if (!context.count); {
And also an extra opening-brace.
These shouldn't have a semi colon on the end:
if (readlpr == dblpr) {
...
} else if(readlpr !== null) {
...
if (!context.count) {
In the end it should look something like this:
if (msg.payload.License_Plate !== null) {
if (readlpr == dblpr) {
opengate = 1;
} else if(readlpr !== null) {
validdigits = 0;
// { <!-- Remove this as well, it's an extra brace
if (!context.count) {
context.count = 0;

Related

if it has footer.text it reacts, but I want it to do the opposite

what it does is add a reaction :pray: when an embed has footer.text, but I don't want that, I want it to react when it doesn't have a footer.text
if(message.embeds.length >= 0)
// Check if the Message has embed or not
{
let embed = message.embeds
// console.log(embed) just a console.log
for(let i = 0; i < embed.length; i++)
// Loop it since in v13 you can send multiple embed in single message
{
if (!embed[i] || !embed[i].footer || embed[i].footer.text === null) return;
// check each embed if it has footer.text or not, if it doesnt then do nothing
{
setTimeout(function(){
message.react(':pray:')
}, 1000);
}
}
}
You simply need to switch your conditions like so:
if (embed[i] || embed[i].footer || embed[i].footer.text !== null) {
setTimeout(function() {
message.react('🙏')
}, 1000);
}
Where !== represents Strict Inequality so what you are actually doing is checking the following conditions
If there is an embed to the message
If that embed object has a footer
If the content of the footer is not empty
Further since all of these conditions are necessary I would suggest using Logical AND (&&) operator which only returns true if the checks of all operands are fulfilled, so your code would look something like this:
if (embed[i] && embed[i].footer && embed[i].footer.text !== null) {
setTimeout(function() {
message.react('🙏')
}, 1000);
}

How to put an Application's value as a variable in javascript?

I have a counter that updates itself everytime the page loads. I want to use that counter's value as a variable in javascript so that I could use it. How do I do this?
if (Application["counter"] == null)
{
Application["counter"] = 0;
}
if (Application["counter"] != null)
{
if (Request.Form["sub1"]==null)
{
Response.Write("<script>alert('You must Enter Number')</script>");
}
if (Request.Form["sub1"] != null)
{
Application["counter"] =(int)Application["counter"]+ 1;
Response.Write(Application["counter"]);
}
}
this is the code for the counter in the cs page.
Whenever I go to write Javascript and try to save it as a variable it gives me an error: Error 16 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement.
var y=<%Application["counter"];>%;
For the server side code, you can use it like below. Just a recommendation. And for front end side, you have typos.
// Server side code - Correct usage
if (Application["counter"] == null) {
Application["counter"] = 0;
}
// there's no need to check Application["counter"] after the code above
if (Request.Form["sub1"] == null)
{
Response.Write("<script>alert('You must Enter Number')</script>");
}
// also, you can use else in here instead of another if, because there isn't any other condition.
else {
Application["counter"] = (int)Application["counter"] + 1;
Response.Write(Application["counter"]);
}
// JS Code
var y = <%= Application["counter"] %>;

How to stop a simulation in contiki with script when all motes(nodes) have a certain output?

This code would stop the simulation when one node(no matter which one) print 'ok'.
WAIT_UNTIL(msg.equals('ok'));
log.testOK();
This code means node 1 print 'ok', then stop.
WAIT_UNTIL(id == 1 && msg.equals("ok"));
log.testOK();
I want to stop the simulation when all nodes already print 'ok', means after each node prints 'ok' at least one time. I tried some codes but it didn't work.
Not work(every node already print ok, not stop) code(suppose I have 10 nodes):
var test = 0;
for (var i=1;i<11;i++)
{
if (id == i && msg.equals("ok")){
test = test+1;
}
}
WAIT_UNTIL(test == 10);
log.testOK();
Not work(every node already print ok, not stop) either:
WAIT_UNTIL((id == 1 && msg.equals("ok"))&&(id == 2 && msg.equals("ok")....(id == 10 && msg.qeuals("ok"));
log.testOK();
Any suggestions? Thanks a lot in advance. It's better if I don't need to specify the total node number in script.
Wait until your condition is true. Once true then call stopSimulation() function of the Simulation class.
/* Stops this simulation (notifies observers). */
public void stopSimulation()
{
if (isRunning())
{
stopSimulation = true;
/* Wait until simulation stops */
if (Thread.currentThread() != simulationThread)
{
try
{
if (simulationThread != null)
{
simulationThread.join();
}
}
catch (InterruptedException e)
{
}
}
}
}

true == false evaluates to true somehow?

I've been working to scrape some webpage that is using the OWASP CRSFGuard project for protection. The library seems to be causing one of my requests to get a 401 so I started digging through their code and noticed the following;
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if(current == target || current == 'localhost') {
result = true;
} else if(true == false) {
if(target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
From what I can tell, there must be instances where this code is executed; result = current.endsWith('.' + target);. Given true == false is inherently false, how would the code reach that statement? Is this some JS oddity (I know we're not using the strict === equality, but seriously...)?
Answer: It will never reach that code block.
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if (current == target || current == 'localhost') {
result = true;
} else if (true == false) {
if (target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
var trueFalse = document.getElementById('trueFalse');
trueFalse.innerHTML = isValidDomain('true', 'false') ? 'WTF!' : 'All is good in the JS World';
trueFalse.addEventListener('click', function(e) {
trueFalse.innerHTML = (true == false) ? 'WTF!' : 'All is good in the JS World Still';
});
<div id="trueFalse"></div>
I would say that Blazemonger is most likely correct.
That else if probably had some other condition at some point, and for whatever reason, they decided they didn't want that block of code to execute anymore, so they changed the condition to something that is always false.
It's also not entirely uncommon to see programmers use 1 === 0 as an indication for false. Why they would want to do this is anybody's guess.

mulitple url hashtags not being read in properly

I have recently nabbed an example of reading in multiple hashtags from a url, while the first split works perfectly fine the second time I try to spilt the values a second time it seems not to be read in as seen I have tried to use the alerts to determine the problem and they stop working shot of the keyValuePair is initialized .
var mangaNumber = 0;
var chapterNumber = 0;
var i, variables = window.location.hash.split(';');
if (variables.length > 0) {
// Variables present in hash
for (i = 0; i < variables.length; i++) {
var keyValuePair = variables.split('=');
if (keyValuePair[0] == mangaNo) {
mangaNumber = unescape(keyValuePair[1]);
alert(mangaNumber);
}
if (keyValuePair[0] == chapterNo) {
chapterNumber = unescape(keyValuePair[1]);
alert(chapterNumber);
}
if (keyValuePair[0] == pageNo) {
pageNumber = unescape(keyValuePair[1]);
alert(pageNumber);
}
}
}
else {
// No variables in the hash
alert('this is a fail foo');
}
Perhaps this is just an error you made when you posted it here, but try this:
var keyValuePair = variables[i].split('=');
Also, unless mangaNo etc actually are variables (and not strings), you need to quote them:
if (keyValuePair[0] == "mangaNo") {
mangaNumber = unescape(keyValuePair[1]);
alert(mangaNumber);
}
...
if (keyValuePair[0] == "chapterNo") {
...
if (keyValuePair[0] == "pageNo") {

Categories

Resources