I have a pdf with two input text boxes. 1.) Item Code and 2.) Item
I am trying to populate "Item" based on "item code" but the nested statements are giving me data for the first else if condition below for all cases. For example, I should get "20% 100 ML" for code 5009113 and "25% 50ML" for code 5009111, and so on. Instead, I am getting "20% 100ML" for any and all values in item code. Please help me with this :)
var v = this.getField("Item Code").valueAsString;
var RXC = Number(v);
if (v=="") event.value = "";
else if (RXC=5009113) event.value = "20% 100ML";
else if (RXC=5009111) event.value = "25% 50ML";
else if (RXC=5009112) event.value = "25% 100ML";
else if (RXC=5009099) event.value = "5% 250ML";
else if (RXC=5009110) event.value = "5% 500ML";
The conditions in your else if statements contain expressions such as the following:
RXC=5009113
This is an assignment expression: you are assigning the value 5009113 to the variable RXC. This is considered to be a "truthy" statement and therefore it evaluates to true and therefore no more else if statements are considered.
Instead of this you should use the comparison operator ===. Also take a look at this question regarding the difference between == and === - and why it is better to use === here.
I personally prefer to use white space characters to separate out the parts of an expression:
else if (RXC === 5009113)
It makes it easier (for me) to see what is going on - and easier to spot where I may have used = instead of ===.
But I would recommend re-working the entire section of code to use "if/else" with braces, as follows:
if (v === "") {
event.value = "";
} else if (RXC === 5009113) {
event.value = "20% 100ML";
} else if (RXC === 5009111) {
event.value = "25% 50ML";
} ...
And probably even better would be to use a switch statement, as mentioned in the comments. This is going to be less cluttered than several if/else statements: easier to read, debug, and maintain.
Related
So I'm new to Javascript, just finished a small free online crash course that covers the bare bones basics but I'm still pretty clueless.
Found TwilioQuest that takes problems and gets the player to solve them in a game format.
One issue has me at the mercy of the same 11 lines of code.
The task is write a script that will take two command line arguments - a pair of strings that should be compared to see which one comes first alphabetically (letter casing is not important). The script should determine if the first string is before, after, or in the same position (equal) to the second string, alphabetically. For each case, you should print out a number with console.log.
When the first argument is earlier in the alphabet than the second, your script should print -1.
When the first argument is the same as the second, your script should print 0.
When the first argument is later in the alphabet than the second, your function should print 1.
This is what I have right now and I'm being told that its false or that it doesn't work
const firstValue = process.argv[2];
const secondValue = process.argv[3];
if ((firstValue.toLowerCase() < secondValue.toLowerCase())) {
console.log(-1)
};
if ((firstValue > secondValue)) {
console.log(1);
};
if ((firstValue.ignoreCase == secondValue.ignoreCase)) {
console.log(0);
};
I have tried nesting the conditional operator and I'm still told its wrong
You're only doing the case-insensitive comparison in the first condition. toLowerCase() doesn't modify the string, so you need to do it in all the comparisons.
But it's easier if you do it once when assigning the variables.
const firstValue = process.argv[2].toLowerCase();
const secondValue = process.argv[3].toLowerCase();
if (firstValue < secondValue) {
console.log(-1);
} else if (firstValue == secondValue) {
console.log(0);
} else {
console.log(1);
}
When you have a series of mutually exclusive tests, you should use else if and else so it doesn't perform unnecessary tests.
you can use charCodeAt() it provides the ascii value of a string character
const firstValue = process.argv[2];
const secondValue = process.argv[3];
firstValue.toLowerCase()
secondValue.toLowerCase()
if ((firstValue.charCodeAt(0) < secondValue.charCodeAt(0))) {
console.log(-1)
};
if ((firstValue.charCodeAt(0) > secondValue.charCodeAt(0))) {
console.log(1);
};
if ((firstValue.charCodeAt(0) == secondValue.charCodeAt(0))) {
console.log(0);
};
hope this solves your problem
I'm not a programmer by any means. I'm an animator trying to use JS expressions in After Effects. I'm getting an "Undefined value used in expression" error on line 1 where I define a variable.I already showed it to my friend on discord who is a cs major, and he had no clue what was wrong with it.
Here's just a paste of the code if you need it:
var count = 1;
if (framesToTime(time) % 12 == 0) {
count = count + 1
if (count % 2 == 0){
thisProperty = 95
} else {
thisProperty = 20
};
} ;
Ok I don't know why the hell this fixed it, but I changed the name of the variable from "count" to just "x" and it works now. Go figure
Try it.
var count = 1;
if (framesToTime(time) % 12 == 0) {
count = count + 1;
if (count % 2 == 0){
thisProperty = 95;
} else {
thisProperty = 20;
}
}
thisProperty;
In your code, thisProperty has become an ordinary variable. If you write its name at the end of the code, then its value will be assigned to the property.
In AE, if there is nothing inside an if statement or the if statement contains malformed/error code you will receive this error. Put a temp value inside the curly braces or something to process and ensure nothing inside will throw an error.
I also received this error with this:
pastTime = timeToFrames(time)-1;
curPos = transform.xPosition;
pastPos = transform.xPosition.valueAtTime(framesToTime(pastTime));
if (curPos-pastPos[0] != 0) {
// Here is the problem in my case. added a value here 99 to fix until finished testing.
}
else {
effect("Angle Control")("Angle")
}
if/else statements are strict
The syntax for if/else statements is strict in the JavaScript engine
and need to be written for standardized JavaScript.
https://helpx.adobe.com/after-effects/using/expression-language-reference.html*
I got this error because there was a missing semicolon.
I'm new to this site and currently doing a computer programming class up in ontario, canada. I have a computer programming class and currently making an interactive tic-tac-toe game. I use if statements in a function in order to verify in theres a winner. I have an If and an else if. However only the first if works and does its code. When i try to do the conditions for do into the else if it doesn't works. However, if i swap my else if and if so that both conditions are swapped. again only first first works and second will never work. so to me it sounds like my conditions are good. i dont know if this makes sense lol
function verifie_gagnant()
{
if (document.getElementById("centregauche").firstChild.classList.contains("markX") &&
document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
document.getElementById("centredroite").firstChild.classList.contains("markX") ||
document.getElementById("centregauche").firstChild.classList.contains("markO") &&
document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
document.getElementById("centredroite").firstChild.classList.contains("markO"))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
else if (document.getElementById("hautgauche").firstChild.classList.contains("markX") &&
document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
document.getElementById("hautdroite").firstChild.classList.contains("markX") ||
document.getElementById("hautgauche").firstChild.classList.contains("markO") &&
document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
document.getElementById("hautdroite").firstChild.classList.contains("markO"))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}
By hearing to your problem it looks like in all the cases both the condition are true. Hence which ever condition is first it executes that first skipping the else part. You can test this removing the else and keeping both in separate if condition. You will notice it navigate inside both if condition. You need to change you IF condition. See below to know more on If condition
IF is a Conditional Statements. ... Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.
You have an || between && conditions which is evaluating to true in both if and else if.
So even if you swap conditions it always execute first one.
Try putting ( condition1 && condition2) || (condition3 && condition4) in both if and elseif conditions
Example fix. (You need to see where actually you need to group conditions)
function verifie_gagnant()
{
if ((document.getElementById("centregauche").firstChild.classList.contains("markX") &&
document.getElementById("centrecentre").firstChild.classList.contains("markX") &&
document.getElementById("centredroite").firstChild.classList.contains("markX")) ||
(document.getElementById("centregauche").firstChild.classList.contains("markO") &&
document.getElementById("centrecentre").firstChild.classList.contains("markO") &&
document.getElementById("centredroite").firstChild.classList.contains("markO")))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
else if ((document.getElementById("hautgauche").firstChild.classList.contains("markX") &&
document.getElementById("hautcentre").firstChild.classList.contains("markX") &&
document.getElementById("hautdroite").firstChild.classList.contains("markX")) ||
document.getElementById("hautgauche").firstChild.classList.contains("markO") &&
document.getElementById("hautcentre").firstChild.classList.contains("markO") &&
document.getElementById("hautdroite").firstChild.classList.contains("markO")))
{
document.getElementById("winner").innerHTML= tours + " a ete vaincu!!";
}
}
This is the first thing you can fix (syntax) after this you need to actually see what is the actual data returned after the single condition is evaluated. Maybe both have same data.
I get the nested if loops (same as using && operator), but how does this code here know which conditions to execute with no conditions and just back to back else statements? One of them is within the nested if statements. I can tell that's obviously why this works the way it does, I just don't get how. Also, I know how to write this in several more readable ways testing multiple conditions. Please just explain what is happening with this code here. How does it know to output "You are too old" or "You are too young?"
var age = prompt("Please enter Your age here :");
var min_age=18;
var max_age=40;
if(age>=min_age){
if(age<=max_age){
console.log("You meet the requirements for this competition");
}else{
console.log("You are too old");
}
}else{
console.log("You are too young");
}
The if-then-else ambiguity is known for a long time. All languages have solved it by defining that an else will match the first perceding if. So:
if (a)
if (b)
x = 1;
else
x = 2;
resolves to:
if (a) {
if (b) {
x = 1;
}
else {
x = 2;
}
}
EDIT by Nisar's reuest:
The if statement is defined as:
if (<condition>) <statement> [else <statement>]
This means that a <statement> in the above may also be an if statement. So, for example:
if (<condition>) if (<condition>) [else <statement>] [else <statement>]
As each else part is optional, the compiler has no way of knowing when it sees an else part to which if it belongs. To solve that the language defines that an else always matches the first preceding if.
The brackets {} set the limit.
Try to think in pseudocode, look beyond the characters and think about what is happening.
Reading in order:
If you are old enough
If your are not too old
'You meet the requirements for this competition'
OTHERWISE
'You are too old'
END
OTHERWISE
'You are too young'
END
Note how indentation can help see the limits of the conditions. Each indented part can be separated.
Firstly, let's indent your code.
var age = prompt("Please enter Your age here :");
var min_age = 18;
var max_age = 40;
if (age >= min_age)
{
if (age <= max_age)
{
console.log("You meet the requirements for this competition");
}
else
{
console.log("You are too old");
}
}
else
{
console.log("You are too young");
}
Starting off..
var age = prompt("Please enter Your age here :");
Let's say you enter 21 in the prompt box, so age=21
We initialize
var min_age = 18;
var max_age = 40;
Now let's look at the first if condition.
if (age >= min_age)
If you substitute the values,this translates to
if (21 >= 18)
This is true,therefore we go inside the if block and not to the else.
The next line is.
if (age <= max_age)
This translates to
if (21 <= 40)
Considering this is also true, we print You meet the requirements for this competition.
The most important take-away from this is, indent your code, and the rest becomes pretty simple.
There are just 3 Options
too young
correct age
too old
First Check - is the person old enough?
if(age>=min_age)
Second check - is the person too old?
if(age<=max_age)
the only possible option left after this if statment is FALSE :
too old
I have topdatedata value as 222
I have these 3 conditions specified
if((topDateData<250)&&(topDateData>25))
{
alert('one');
}
else if((topDateData>300)&&(topDateData<300))
{
alert('Two');
}
else
{
alert('Three');
}
My questions is why is it getting the value as alert(3) and not alert(one)??
When explicitly setting the value to 222, I see 'one' get alerted: http://jsfiddle.net/Wvjfa/
You should debug your actual value ( alert(topDateData); if you like) and see if it really is what you think it is.
Beyond that, Matt Ball is right, your second condition is borked. Also lonesomeday and Kerry are right about your variable case not matching between your question and the posted code.
Javascript is case sensitive, is topdatedata = 222 or topDateData = 222?
it's much safer just to set one value - as you're second criteria looks dodgy
var topDateData = 222;
if(topDateData > 300)
{
alert('two');
}
else if(topDateData > 250)
{
alert('');
}
else if(topDateData > 25)
{
alert('One');
}
else
{
alert('Three');
}
start with the one that's hardest to satisfy (in this example the highest) and then work your way down. It's much easier to follow. Other than that it should work, as per the other comments on here
My guess is that you have a string.
var trueBool = '222' < '250'; //true
var falseBool = '222' > '25'; //false
To debug if topDateData is a String or not, do the following:
alert(topDateData + 1);//outputs '2221' if a string, and '223' if a number
Here's a fiddle showing the difference.
UPDATE:
I've tested alert(('222' < 250) && ('222' > 25)); and that outputs true. However, I'm not sure all JavaScript compilers will be smart enough to convert the string to a number first. You should run the same test and see if true is the output on your browser.
It looks like your topDateData variable contains a string value "222" instead of an integer.
You could try to cast to an integer this way:
topDateData = parseInt(topDateData);
...