Issue with using Greater Than or Equal to - zapier

So I have tried this a number of ways but every time I have someone score above 80 it returns Failed and not Passed...
First Way:
if (inputData.score >= '80') {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}
Second Way:
if (inputData.score >= '80') {
output = 'Passed';
} else {
output = 'Failed';
}
return {result: output};
However, if someone gets 80 it will return a Pass... I am at a loss.

Tried removing single-quotes from '80'? You are checking a string against greater-than operator.
if (inputData.score >= 80) {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}

David here, from the Zapier Platform team.
You definitely want to be comparing numbers to numbers. As the other answerer mentions, convert your incoming score to a number and compare it to a number.
if (parseInt(inputData.score, 10) >= 80) {
return {result: 'Passed'};
} else {
return {result: 'Failed'};
}
As for which of the two return methods to use, they're equal as far as Zapier is concerned. I find the first a little cleaner, so I went with that.

Related

Deprecated Functionality: usort(): Returning bool from comparison function is deprecated, return an integer less than, equal to, or greater than zero

Ran into a slight issue here below with some of my code.
// sorting
$sortField = $this->sortField;
$sortDir = $this->sortDir;
usort($data, function ($a, $b) use ($sortField, $sortDir) {
if ($sortDir == "asc") {
return $a[$sortField] > $b[$sortField];
} else {
return $a[$sortField] < $b[$sortField];
}
});
A bit confused here on what i need to change.
I read this in another thread.
PHP 8 introduced the Stable Sorting RFC, which (as it sounds) means that all sorting functions in PHP are now "stable".
The spaceship operator is used for comparing two expressions. It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b. Comparisons are performed according to PHP's usual type comparison rules.
So does this mean I need to add the spaceship operator here in the returns:
return $a[$sortField] <=> $b[$sortField];
} else {
return $a[$sortField] <=> $b[$sortField];
}
That is it?

[Clang RecursiveASTVisitor]How to make a distinction between 'If' statement and 'Else If' statement?

I'm using clang's RecursiveASTVisitor to parse some C code. I override visitStmt() and want to make a distinction between 'If' statement and 'Else If' statement.
Example:
if(a > 0){
XXX
}
else if(a == 0){
YYY
}
else{
ZZZ
}
In visitStmt(), I use:
if(isa<IfStmt>(S))
to judge whether a statement is IfStmt.
But how can I know the statement is 'Else If' rather than 'If'?
I want to insert different stubs(XXX under 'If' and YYY under 'Else If').
ps: I'm not a native English speaker. Please forgive me for my poor express.
In C there is not specific else if statement, because
if (a) {
...
}
else if (b) {
...
}
else {
...
}
is conceputally (and in terms of an AST) something like the following
if (a) {
...
}
else {
if (b) {
...
}
else {
...
}
}
So the first snippet is sort of a "hidden nesting", because the body of the first else just consists of a single if ... else statement, so you can omit the {} around it. And the last else does not belong to the first if but to the second.
So what you probably can do to determine whether you have an else if or not, is step up in the tree, and check if the parent node of the current node is an else statement and the if is the first (or only) statement in that parent-else's body.
There are other languages (for instance VB) which have a If .. ElseIf .. Else construct. But C doesn't.

Retuning multiple variables with Zapier

I need to be able to return a few variables based on a code. If code A then return these. If Code B then return these. In no code, return these.
This is what I have so far.
if (inputData.code === 'WSDCD-D2DUK') {
output = 'Company A';
} else if (inputData.code === '6P1CX-5U2TY'){
output = 'Company B';
}
else {
output = 'Not Avaliable';
}
return {result: output};
And what I need is something like this:
if (inputData.code === 'WSDCD-D2DUK') {
output = 'Company A';
course = 'ABC'
} else if (inputData.code === '6P1CX-5U2TY'){
output = 'Company B';
course = 'XYZ'
}
else {
output = 'Not Avaliable';
course = 'Not in one';
}
return {result: output, course};
Looks like you did not define the course variable. However, output doesn't need to be defined as it is available in the Code by Zapier scope.
I've modified the code a slight bit to reflect your use-case.
var inputData = {'code': 'D2DUK'}
//Remove the line above before pasting in the Code step.
//You will need to configure it in the Zap.
//Defining the variables here.
let course = '';
let company = '';
if (inputData.code === 'WSDCD-D2DUK') {
company = 'Company A';
course = 'ABC'
} else if (inputData.code === '6P1CX-5U2TY'){
company = 'Company B';
course = 'XYZ'
}
else {
company = 'Not Avaliable';
course = 'Not in one';
}
//Returning the JSON object with the keys company and course.
return {company, course};

How to keep a Swift loop running indefinitely (forever)?

I have written a function which currently displays all of the prime numbers under 1000.
I can keep making 1000 bigger to generate more numbers, but I do not know how to make it keep going on forever once run.
func generatePrimes() {
let numbers = 2...1000
for n in numbers {
var prime = true
for i in 2..<n {
if n % i == 0 {
prime = false
break
}
}
if prime == true {
print(n)
}
}
}
You could use a while loop with the argument being true. That way, the loop would never end.
func generatePrimes() {
var n = 2
while true {
var prime = true
for i in 2..<n {
if n % i == 0 {
prime = false
break
}
}
if prime == true {
print(n)
}
n += 1
}
}
Note that we use a variable (n) and increment (add 1 to it) on each iteration of the while loop.
If you want it to end when it hits some n, you can have a conditional that checks against n and break's out of the loop:
var n = 2
while true {
if n == 1000 {
break
}
n += 1
}
The above code will stop execution when n hits 1000 and will not print anything for that case.
Use a while loop instead of for loop. It can also be done with a for loop, but for loop is preferred when you know the number or range of the iterations you are performing. If you're using a while loop you just have to set the condition for the loop, in this case you need to specify the condition as true since you want the loop to run forever.
Just replace your for loop in your code with while. It should look something like this
while(true)
{
if n % i == 0 {
prime = false
break
}
}
You also can build an unconditional infinite loop using do {} block and continue statement.
AGAIN:
do {
print("step...")
continue AGAIN
}
func generatePrimes() {
var prime = true
let numbers = 2...1000
for n in numbers {
prime = true
if n == 2 || n == 3 {
print(n)
} else {
for i in 2..<n-1 {
if n % i == 0 {
//not prime numbers
prime = false
}
}
if prime == true {
print(n)
}
}
}
}
You can do it like this. Just exclude 1 and n.

Is there a language where we can mark value as result and then continue function/method body?

I was reading smalltalk tutorial and this idea came to my mind.
Assume we have some language and there instead of return we're marking some value as final return value and then we continue method, once method end is reached - no matter what else was called we're returning marked value unless something else specified manually like this:
Let's say ^^ is a operator that marks value for return if no explicit return is found till end of method
m1()
{
^^1;
some_other_code;
}
m2(par)
{
^^2;
if par == 1: return 1;
some code;
}
m3(par)
{
if par == 1: return 1;
else if par == 0: ^^0;
do some stuff;
if par < 0: return -1;
}
m1() should return 1
m2(0) should return 2
m2(1) should return 1
m3(0) should return 0
m3(1) should return 1
m3(-2) should return -1
This is a little similar to ruby's tap() but not the same
Pascal did that. The return value was set by assigning to the function name. Swift does something not quite the same: You can mark code anywhere that will be executed when the function exits. Your return statement exists, but only after other code written somewhere else gets performed.

Resources