My simple if...else statement below results in the true condition! I have simplified this from a project I am working on just to see if I would get the same illogical result that I was getting in my project. And I used a fresh spreadsheet and script. What is going on here? I am totally baffled! It's a trivial function just to prove my point. The toast pops up with "aNumber is 7" even though I have defined it with a value of 5 few line above!
Any explanation? I have been searching but cannot find this problem online. Hope you can enlighten me.
function testFunction(ss) {
var aNumber = 5
if (aNumber = 7){
SpreadsheetApp.getActiveSpreadsheet().toast("aNumber is 7")
}else{
SpreadsheetApp.getActiveSpreadsheet().toast('aNumber IS NOT 7')
}
}
I'm assuming this is Javascript You are assigning 7 to aNumber, the comparison operator is '==', like most C-style languages.
You should use == to check for IF conditions (in case of Javascript)
if (aNumber == 7)
In C and similar languages aNumber = 7 is an assignment returning 7 and 7 is interpreted as true (only 0 is false).
Use aNumber == 7 for comparison.
Use = when setting the value for a variable.
Use == when comparing two values.
Try this:
function testFunction(ss) {
var aNumber = 5
if (aNumber == 7){
SpreadsheetApp.getActiveSpreadsheet().toast("aNumber is 7")
}else{
SpreadsheetApp.getActiveSpreadsheet().toast('aNumber IS NOT 7')
}
}
Related
I have a PDF form that adds up several different answers and displays the sum of these answer at the bottom of the page. I want to take that number, and have a sentence underneath it display three different options depending on the sum.
Greater than 60: Proceed
between 45 & 60: Consult Sales Lead
Less than 45: Decline
I am attempting to run a custom calculation script that takes the sum (which is named "total") and writes the above options, but I'm running into a myriad of errors.
My code I've written is below
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
} else {
if (A <= 45){
event.value = "Decline";}
} else {
if (A < 60 && A > 45){
event.value = "Proceed, decision made with sales leader";}
}
If I were to only write the below block, I do not get any errors.
var A = this.getField("total").value;
if (A >= 60){
event.value = "Proceed";
}
I'm a newbie when it comes to most JavaScript, so any help is greatly appreciated! Thanks in advance!
I have based most of my code off of different search results from google. My main source
example 1
Below are a few other links I've referenced
example 2
example 3
You can leave away the
if (A < 60 && A > 45) {
line, because that condition is always true after the two previous conditions.
And then, it should work properly.
Just if you want to stay with that last condition, you would have to close it with a curly brace.
I think I managed to figure it out! For some reason, the addition of the "else" factor was causing syntax errors, so I tried it without and it seems to work as intended!
My code, for anyone that happens to find this, is the following:
var A = this.getField("total").value;
if (A >= 60) {event.value = "Proceed";}
if (A <= 59 && A >= 46) {event.value = "Proceed, decision made with sales leader";}
if (A <= 45) {event.value = "Decline";}
if (A == 0) {event.value = " ";}
Thanks to everyone that took a look at this, even if you didn't get to comment before I figured it out!
I'm learning reverse engineering, and I have the following snippet which I am trying to make sense of:
var = strcmp("C:\\Windows\\System32\\svchost.exe", pe.szExeFile);
if (var)
var = -(var < 0) | 1;
if (var)
{
// additional code here
}
I think I understand most of what is going on here, but I'm confused about the purpose of the
var = -(var < 0) | 1; line. I'm only very vaguely familiar with C/C++, so I'm having a hard time wrapping my head around what this line does.
I understand that it's a bitwise OR, but I'm unsure how the -(var < 0) works. Is the expression inside the parentheses evaluated to a 1 or 0 and then the negative is applied and the OR? Is it evaluated as a boolean? If so, how does the | work on a boolean?
Or am I totally missing the point here?
strcmp() returns one of three possible results:
< 0
0
> 0
Assumed common two's complement, after the first if the variable var will be
-1 for the former "< 0"
0 for the former "= 0"
+1 for the former "> 0"
However, the second if will be taken only if var is non-zero.
The "mysterious" first if has no effect, as far as the source is concerned that you show.
In the university I had a quiz today. The quiz is over but I can't understand some of its questions are their correct answers.
Note: I am not asking this to solve my quiz. Quiz is over I am just confused in some questions.
Question 1:
Consider the following variable declarations:
int catHeight = 6;
int dogHeight = 7;
string dogName = "Rover";
string catName = "Sylvester";
float catWeight = 15.0;
float dogWeight = 20.0;
bool dogRabies = true;
bool catRabies = false;
Choose Boolean expressions equivalent to following statements.
the cat has rabies and does not weigh 20 pounds or less
catRabies && catWeight > 20
!( catRabies && catWeight <=20)
! catRabies && catWeight >=20(This was marked as correct. I think the first option is correct)
the cat height and the dog height are not 10 (Hint: more than 1 answer)
catHeight > 10 && dogHeight >10
(catHeight && dogHeight) != 10
catHeight !=10 && dogHeight != 10
2nd and third are were marked as correct in result. But I think that only third one is correct. Please explain if I am wrong.
Question 2:
if numNeighbors >= 3 || numNeighbors = 4
++numNeighbors;
cout << "You are dead" << endl;
else
--numNeighbors;
What is wrong with the following if statement (there are at least 3 errors). The
indentation indicates the desired behavior
syntax error; else without previous if(marked as correct)
syntax error; value required of left operand (marked as correct)
syntax error; Parenthesis missing (marked as corrent)
syntax error; statement missing
I understand why 1 and 3 are correct but can't get the meaning of second one. Kindly explain it.
3 errors in question 2:
missing ( ) around the if condition
in the second part of the if condition there must be double ==
{ } are missing
To be a valid code it must be set like this:
if (numNeighbors >= 3 || numNeighbors == 4)
{
++numNeighbors;
cout << "You are dead" << endl;
}
else
--numNeighbors;
This was marked as correct. I think the first option is correct
Yes, you're right.
But I think that only third one is correct.
You're also right here.
Question 2
This one does not make sense unless you are trying to parse like a compiler. For instance, "else without previous if" only makes sense if you consider the current state of the code and not what you are trying to achieve. But the question tells you what you are trying to achieve.
syntax error; value required of left operand (marked as correct)
This means the condition is being parsed as (numNeighbors >= 3 || numNeighbors) = 4; which makes clear that the left side is not something you can assign to.
Your understanding of (1.1) and (1.2) seems to be correct.
In (2), if you fix the other errors,
if (numNeighbors >= 3 || numNeighbors = 4)
will be parsed as
if ((numNeighbors >= 3 || numNeighbors) = 4)
For this GCC outputs error: lvalue required as left operand of assignment, which reads similar to "value required of left operand".
I am new to C++ and I have a problem where i have to transform a pseudocode in C++ / C / Pascal language. The answer at the end of the book written in Pascal.
The problem in my C++ code is that at the line 12, I get the error which can be found in the title. Any idea?
Pascal Code:
var n,x:integer;
begin
n:=0;
repeat
write('x=');read(X);
if x<>0 then
if x mod 5 = 0 then
n:=n+1
else
n:=n-1;
until x=0;
if n=0 then
write('yes')
else
write('no')
end;
My C++ Code:
int main()
{
int x,n;
cin>>x;
while(x>0)
{
if(x>0)
{
if(x%5=0){
n=n+1;
} else {
n=n-1;
}
}
if(n=0){
cout<<"Yes"<<;
} else {
cout<<"No"<<;
}
}
}
You have a simple typo: if(x%5=0){ is an attempt to assign 0 to x % 5 (due to operator precedence modulus is computed before assignment). x % 5 cannot be assigned to (it's not an lvalue) and the compiler is telling you that.
The fix, of course, is to write x % 5 == 0.
You're lucky in this case that the error is picked up at compile-time. Something like if (n = 0) (on line 18) might not be, since x = 0 is an expression with value 0.
Two ways to guard against that:
Ensure that your compiler warnings are as aggressive as you can bear. With gcc, I use -Wall -Wextra, and that combination is enough to catch this common problem.
Some developers will write if (0 == x) since an errant if (0 = x) would be picked up at compile time as an attempt to assign to 0. Personally, I find that obfuscating.
Assignment operator requires lvalue means the left side operand need to be a variable/location that can hold a value.
This is what is meant by the error.
What you need in your if statement is == likely not assignment as mentioned by other answers
You need to use == in conditions (while, if, ...) for equality check in C++.
if(x%5 = 0)
should be
if(x%5 == 0)
"x%5" is not an lvalue in that you can not assign a value to it, hence the error.
In C++ want to write something like this
int Answer;
if (Answer == 1 || Answer == 8 || Answer == 10)
and so on, is it any way to make code shorter without repeating variable always?
Try:
switch (Answer) {
case 1: // fall through
case 8: // fall through
case 10:
// ... do something
break; // Only need if there are other case statements.
// Leaving to help in mainenance.
}
For readability I'd encapsulate the logic in descriptively-named functions. If, say, your answers are things with a particular color, and answers 1, 8, and 10 are green things, then you can write that logic as
bool ChoiceIsGreen(int answer)
{
return (answer == 1 || answer == 8 || answer == 10);
}
Then your function becomes
if (ChoiceIsGreen(Answer))
{
// offer some soylent green
}
If you have a lot of choices like this, I can see it getting hard to read if you have a lot of raw numbers all over the place.
If and only if you need to optimise for code size manually, and Answer is guaranteed to be positive and less than the number of bits in an int, you might use something like
if ( ( 1 << Answer ) & 0x502 )
But normally you don't want to obscure your logic like that.
You could put the values into a container and search the container.
Sounds like a std::set would be a wise choice:
if answer is in the set of (1, 8, 10) then do....
Remember that a std::set must be initialized during run-time, unlike numeric constants or an array of numeric constants. Before making any performance changes, first get the program working correctly, then profile if necessary, that is only if the program demands performance optimization.