Code:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value!GenderField.value) {
alert('No criteria Added');
return;
}
The alert is not called when all the text fields are blank.
You're missing the && between the last two criteria
It should be:
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value)
In cases like this, it makes a lot of sense to format your if statement like this:
if ( !IDTextField.value
&& !FirstNameField.value
&& !LastNameField.value
&& !DateOfBirthField.value
&& !GenderField.value)
If you do it this way, you can't make the mistake you just made.
xbonez got it right. You are missing && between last two expression.
For something not so important, I would like to get all expressions evaluated using || and then add negation using !, rather than negating all expression and evaluate them with &&. This can make this expression a little faster, if am not wrong.
if (!(IDTextField.value || FirstNameField.value ||
LastNameField.value || DateOfBirthField.value || GenderField.value)) {
alert('No criteria Added');
return;
}
Tell me what you all think??
What is this little abomination?
... !DateOfBirthField.value!GenderField.value
I think that should be:
... !DateOfBirthField.value && !GenderField.value
You should write your code like this
if (!IDTextField.value && !FirstNameField.value &&
!LastNameField.value && !DateOfBirthField.value && !GenderField.value) {
alert('No criteria Added');
return;
}
You're missing the && between the last two criteria
the && is missed ,add it and try ,should work if no other errors exist
Related
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 have what I thought would be a simple logic check. In my code
$scope.seatMap.PlaneTypeCode = "175"
However, when I set
$scope.seatMap.PlaneTypeCode === "175" //my debugger returns <b>false </b>
parseInt($scope.seatMap.PlaneTypeCode,10) ===175 // equals 17
I added a few zeros on the radix but that did nothing to help.
I am not sure how to do a comparison check. Any insight on this would be hugely appreciated.
Here is my full if statement
if (parseInt(col.name,10) ===4 && parseInt($scope.seatMap.PlaneTypeCode,10) ===175 && $scope.TripSummary) {
col.available = false;
}
****** Changed my response to this
if (parseInt(col.name,10) ===4 && $scope.seatMap.PlaneTypeCode ==="175" && $scope.TripSummary) {
col.available = false;
} // still getting false
=== is a best practice, you should use it. Review the reference provided by #Joyson
You don't need the ,10 in parseInt because it is the default.
var PlaneTypeCode = "175";
if (parseInt(PlaneTypeCode) === 175) {
console.log('equal');
}
If PlaneTypeCode is a code and can contain anything other than digits, a better comparison would be:
if (PlaneTypeCode === "175")
You can use == instead of ===
$scope.seatMap.PlaneTypeCode == "175"
Please refer to Difference between == and === to know more
use angular.equals($scope.seatMap.PlaneTypeCode,"175")
I am trying to get a comparison operator to work, without success. The operator compares two arrays to ensure they are identical.
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)) {
alert("all matches dropped");
}
The code works of course with 'true' in place of the comparison.
if (true) {
alert("all matches dropped");
}
The strange part is that the comparison returns 'true' when entered into the console:
console.log($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)
----> true
Any ideas what may be wrong? Thanks.
It should be:
if($(array_1).not(array_2).length === 0 && $(array_2).not(array_1).length === 0)
Instead of:
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0))
Here $(array_1).not(array_2).length and ($(array_1).not($(array_2)).length both are not the same thing.
When I make a conditional on jQuery that specifies to make something if an input OR a select list is empty, jQuery works fine:
if((($('input[name=su_name]').val())=="") || ($('select[name=su_family]').val())=="0")
{...}
But when I try to make it check 3 fields (if one, or the other, or the other is empty), I have a syntax error focusing the second "||". Is it not possible to set two "||" (OR) on the same conditional? This does not work:
if((($('input[name=su_name]').val())=="") || ($('select[name=su_family]').val())=="0") || ($('input[name=su_abbrev]').val())=="")
{...}
I don't know why you're using so many parenthesis in that second example, but this should work:
if ( $('input[name=su_name]').val() == "" || $('select[name=su_family]').val() == "0" || $('input[name=su_abbrev]').val() == "" )
You've used so many redundant (), instead you can just do:
if ($('input[name=su_name]').val() == "" || $('select[name=su_family]').val() == "0" || $('input[name=su_abbrev]').val() == "") {
// Your code here
}
I'm trying to adapt a calculator that was made by someone else who used to work at my company. Unfortunately I can't get hold of him at all and was wondering if someone here could help me. My javascript knowledge is limited so please bear with me if this sounds a stupid question or if I'm missing something obvious. I can't find any info on this elsewhere, so any help would be much appreciated!
Here is the current code for the section I want to change:
function date(){
var the_date = new Date();
enter code herevar the_year = the_date.getFullYear();
ret_age=Number(document.calculator.year.value);
gender=Number(document.calculator.sex.value);
a=the_year-ret_age;
if (a<=36)
{
document.calculator.number3.value=68;
}
else if (a>36 && a<=45)
{
document.calculator.number3.value=67;
}
else if (a>=45 && a<=60)
{
document.calculator.number3.value=66;
}
else if (a>60 && gender==1)
{
document.calculator.number3.value=65;
}
else if (a>60 && gender==0)
{
document.calculator.number3.value=65;
}
else
{
alert("Our calculator is having trouble working out your state
pension age. You have been given a default age of 68. Feel free to
change it.");
document.calculator.number3.value=68;
}
}
I want to add in these new 'else if' options, but I think that there is a problem with the number of conditions in each.
else if (a>60 && gender==0)
{
document.calculator.number3.value=65;
}
else if (a>60 && <=62 && gender==0)
{
document.calculator.number3.value=62;
}
else if (a>63 && gender==0)
{
document.calculator.number3.value=60;
}
Am I doing something wrong with trying to use eg. a<61 && <=63 alongside the gender condition? This appears to only work when I say one condition for the number, eg. a<60
Is there any way to use these two conditions in the else if, or will I have to do something different?
It would be great if someone could help - again, apologies if this question is poorly explained. If you need any more info to help then please let me know! I'm not sure what I'm doing. Thank you! :)
You missed an 'a' after '&&' on this condition: else if (a>60 && <=62 && gender==0)
The && indicates that another condition most be true in order for the if statement to validate to true. It does not apply a condition statement to the previous variable.
For example if you had two if statements as follows:
if(x > 1){
if(y < 1){
// do something
};
};
They could be combined into a singly if statement like this:
if(x > 1 && y < 1){
// do something
};
Essentially both sides of the && (or || for an "or" condition) need to be able to evaluate outside of the other statements.
So in your case as previously stated in another answer, you need to changes your if statement from:
else if (a>60 && <=62 && gender==0)
{
document.calculator.number3.value=62;
}
To:
else if (a>60 && a<=62 && gender==0)
{
document.calculator.number3.value=62;
}
your coditions inside the if statements are wrong
replace else if (a>60 && <=62 && gender==0) with else if (a>60 && a<=62 && gender==0)