Whats wrong with my if else statement - boolean-logic

I have a $name variable containing a string, and when I test it, I never get the "Name should be between 2 and 40 characters" error even when it's less than 2 characters long or more than 40. Why?
if (strlen($name) < 2 && strlen($name) > 40) {
$nameError = 'Name should be between 2 and 40 characters';
}

How can the length be less than 2 and greater than 40? You want || ("or"), not && ("and").
if (strlen($name) < 2 || strlen($name) > 40) {
// -------------------^^

Related

Set Script Properties Instant or After Execution?

I have a script that needs to execute at the top of the hour every day.
I set up automatic time triggers that execute a checking script every 10 minutes to see if it's 10 minutes before the next hour. The script sends out text and email to our list and it works (although it takes 5-20 minutes to execute).
The problem is that people are receiving double text and emails.
I suspect the reason is that the checker script sets a script property, but it doesn't actually get set until the completion of the script (similar to writing cells). If the script takes longer than 10 minutes to execute, the script will start executing again without the new property and cause it to run double. My question is: "does the setProperty() happen instantly or does it happen at the end of the script"?
If it happens instantly, then I need to find another reason the script seems to exectute each one twice. Here's my checker script for reference. Note that when I execute function sendComs(time) manually, without the time triggers, it only executes once. So it seems the propblem is something below.
function sendDaily () {
var today = new Date();
var hours = today.getHours();
var mins = today.getMinutes();
var scriptProperties = PropertiesService.getScriptProperties();
var timeLastTriggered = scriptProperties.getProperty('LastTime');
switch (timeLastTriggered) {
case '0':
if (hours >= 4 && mins >= 50 || hours >= 5) {
scriptProperties.setProperties({'LastTime':'5'})
sendComs('5:00 AM');
}
break;
case '5':
if (hours >= 5 && mins >= 50 || hours >= 6) {
scriptProperties.setProperties({'LastTime':'6'});
sendComs('6:00 AM');
}
break;
case '6':
if (hours >= 6 && mins >= 50 || hours >= 7) {
scriptProperties.setProperties({'LastTime':'7'});
sendComs('7:00 AM');
}
break;
case '7':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.5'});
sendComs('7:30 AM');
}
break;
case '7.5':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.6'});
sendComs('7:40 AM');
}
break;
case '7.6':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.7'});
sendComs('7:50 AM');
}
break;
case '7.7':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'7.8'});
sendComs('7:55 AM');
}
break;
case '7.8':
if (hours >= 7 && mins >= 30 || hours >= 8) {
scriptProperties.setProperties({'LastTime':'8'});
sendComs('8:00 AM');
}
break;
case '8':
if (hours >= 8 && mins >= 50 || hours >= 9) {
scriptProperties.setProperties({'LastTime':'9'});
sendComs('9:00 AM');
}
break;
case '9':
if (hours >= 11 && mins >= 50 || hours >= 12) {
scriptProperties.setProperties({'LastTime':'12'});
sendComs('12:00 PM');
}
break;
case '12':
if (hours >= 14 && mins >= 50 || hours >= 15) {
scriptProperties.setProperties({'LastTime':'15'});
sendComs('3:00 PM');
}
break;
case '15':
if (hours >= 17 && mins >= 50 || hours >= 18) {
scriptProperties.setProperties({'LastTime':'18'});
sendComs('6:00 PM');
}
break;
case '18':
if (hours >= 20 && mins >= 50 || hours >= 21) {
scriptProperties.setProperties({'LastTime':'21'});
sendComs('9:00 PM');
}
break;
case '21':
if (hours >= 23 && mins >= 50 || hours < 5) {
scriptProperties.setProperties({'LastTime':'0'});
sendComs('12:00 AM');
}
break;
}
}
I think this will answer the question for you. Paste it and run it.
function doesItHappenInstantly() {
var ps=PropertiesService.getScriptProperties();
ps.setProperty("testvalue",0);
for(var i=0;i<5;i++) {
var tv=ps.getProperty("testvalue");
if(tv!=i) {
SpreadsheetApp.getUi().alert("It does not happen instantly");
return;
}
ps.setProperty("testvalue",i+1);
}
SpreadsheetApp.getUi().alert("It happens instantly");
}
Warning: Don't run this with large number of iterations or you will get the error service invoked too many times for one day.
Properties are stored instantly.
Your script must have other problems that are causing the double messages.

How to update the 3rd element of nested elements (JSON like)?

I currently have a MySQL table as follow:
|| ID || Name || handling || enabled ||
|| 1 || bob || { 2 { 4, 7, 0.2 } 7 { 20.102, 3 } } || 1 ||
|| 2 || abc || { 6 { 4, 9, 0.6 } 7 { 20.102, 83 } } || 1 ||
|| 3 || xyz || { 2 { 4, 78, 0.2 } 7 { 20.102, 3 } } || 1 ||
I'm trying to find a way to do the following trick via an SQL query:
The third number there (7 for id 1, 9 for id 2, 78 for id 3) has to be changed to '30'. I'd do it all manually, but it's a table of approx. 5000 rows. And I "could" make a loop in c++ to do it all, but for some technical reasons, I rather have a SQL query.
If you know that the part to replace is always between first and second comma then you can do some string slicing, like this:
to change text between 1 and 2 comma:
UPDATE TEST_TABLE6
SET A = CONCAT(SUBSTRING_INDEX(A,',',1),',','30',SUBSTRING(A, LENGTH(SUBSTRING_INDEX(A,',',2))+1));
to change text between 7 and 8 comma:
UPDATE TEST_TABLE6
SET A = CONCAT(SUBSTRING_INDEX(A,',',7),',','30',SUBSTRING(A, LENGTH(SUBSTRING_INDEX(A,',',8))+1));

Why exactly the output of this expression return true

I have this expression:
!(1 && !(0 || 1))
The output returns 1 true. And that's ok. When I read the expression I came to the same conclusion before checking the output. But I would really appreciate if someone can explain to me why the returning value is true, that way, I will have a better understanding of boolean logic and how to implement better evaluators in my code.
Key observation here: ! is not, && is the "And" operator, and || is the "Inclusive Or" Operator.
What are you really asking when you say "why it's true?".
0 = false
1 = true
AND && table
0 0 -> 0
0 1 -> 0
1 0 -> 0
1 1 -> 1
OR || table
0 0 -> 0
0 1 -> 1
1 0 -> 1
1 1 -> 1
NOT ! table
0 -> 1
1 -> 0
With parentheses implying "do this first", the statement reduces using the tables above:
!(1 && !(0 || 1))
!(1 && !1)
!(1 && 0)
!0
1
But I don't know "why" it's true. Because that's what an AND operation is, what an OR operation is, and what a NOT operation is, and how reducing a statement works. With those definitions, it can't be another answer, so it's that answer. But you already know that, because you did it yourself and got the same answer ... so what does the question mean?
The innermost expression (0 || 1) is always true.
So !(0 || 1) is always false.
That leaves 1 && 0, which is always false.
So !(false) is always true.
Please forgive my freely intermixing 0/false and 1/true.
The human evaluator (:-).
Working through the expression, following order of operation:
!(1 && !(0 || 1))
= !(1 && !(1))
= !(1 && 0)
= !(0)
= 1
Step by step explanation:
1 = true
0 = false
Starting point: !(1 && !(0 || 1))
Lets start with the inner most expression: !(0 || 1)
Var1 || Var2 =
Var1 or Var2 =
If Var1 or Var2 is 1 or both are 1, the result is 1.
(0 || 1) = 0 or 1 -> the second variable is 1 so the expression is 1.
Insert the result (0 || 1) = 1 into Startingpoint: !(1 && !(1))
! = not (inverts the value of what is behinde)
!1 = 0
!0 = 1
!(0 || 1) = !(1) = 0
Insert the result !(1) = 0 into Startingpoint: !(1 && 0)
So we have !(1 && 0)
Var1 && Var2 = And =
the opossite of or =
If Var1 AND Var2 are both 1, the result is 1. Else it is 0 =
If Var1 or Var2 is 0, the result is zero
1 && 1 = 1
1 && 0 = 0
everything else: 0
So this is left: !(0)
Reminder: ! = not = inverts the expression behind it. So !0 = 1 (and !1 = 0)
This is 1. Or in your case: true
A good book for Beginner C programmers and people who want to learn about programming and logic in an easy, understandable way:
C for Dummies by Dan Godkins
!(1 && !(0 || 1))
Since, you have used parenthesis, evaluation takes place according to them.
First, evaluate innermost parenthesis.
0 || 1 => always true.
!(0 || 1) => !(true) => always false.
1 && !(0 || 1) => 1 && false => always false.
!(1 && !(0 || 1)) => !false => always true.

AS3 convert a positive number to 1 and a negative number to -1

There is a simple trick to convert a number to 1 or -1.
Just raise it to the power of 0.
So:
4^0 = 1
-4^0 = -1
However, in AS3:
Math.pow( 4, 0); // = 1
Math.pow(-4, 0); // = 1
Is there a way to get the right answer without an if else?
This could be done bitwise.
Given the number n (avg time: 0.0065ms):
1 + 2 * (n >> 31);
Or slightly slower (avg time: 0.0095ms):
(n < 0 && -1) || 1;
However, Marty's solution is the fastest (avg time: 0.0055ms)
n < 0 ? -1 : 1;
Not sure if without an if/else includes the ternary operator in your eyes, but if not:
// Where x is your input.
var r:int = x < 0 ? -1 : 1;
Will be more efficient than Math.pow() anyway.

Boolean Logic with If case

There are 2 cases given in the question and on that basis we have to answer.
Cases:
if((NOT(value>=1) OR NOT(value<=10))
if((NOT(value>=1) AND NOT(value<=10))
Now the questions are:
which case you are going to use if the given value either is 1 or 10 ?
which case you are going to use if the given value must be 1 or 10 ?
the problem is whether I takes 1 or 10 I am getting same answer in both the cases. That is if(0) and thus if statement is false in both the cases.?
(NOT(value>=1) OR NOT(value<=10)) = (value < 1) OR (value > 10)
This case is true for [-Infinity ... 0] or [11 ... + Infinity]
Is false for 1 or 10
((NOT(value>=1) AND NOT(value<=10)) = (value < 1) AND (value > 10)
This case is always false, as no number can be smaller than 1 and bigger than 10 the same time.