Checking whether ANY url is true - javascript

I am new to Javascript as well as Jquery , but can not figure out what I am doing wrong. I just want to check if the user is on any of 3 URLs. I just want to check if the user is on either the ABOUT US, MEMSTAFF TEAM or CAREERS sections. That is it. I thought that if I just used the OR (||) operator, this should work. What am I doing wrong?
<script type="text/javascript">
$(document).ready(function() {
// Check if any of these relative URLS are true
if(window.location.href.indexOf("/about-us" || "/memstaff-team" || "/careers") > -1) {
// Alert me if I am in one of the MAIN sections
alert("Your are in one of the MAIN sections");
}
});
</script>

The test
if (window.location.href.indexOf("/about-us" || "/memstaff-team" || "/careers") > -1)
is equivalent to doing
temp = "/about-us" || "/memstaff-team" || "/careers";
if (window.location.href.indexOf(temp) > -1)
Since the || operator just returns the first truthy value, it's effectively doing temp = "/about-us" and just testing for that. "OR" expressions aren't automatically distributed, you need to do it explicitly.
if (window.location.href.indexOf("/about-us") > -1 ||
window.location.href.indexOf("/memstaff-team") > -1 ||
window.location.href.indexOf("/careers") > -1)
But a simpler way is to use a regular expression:
if (window.location.href.match(/\/(about-us|memstaff-team|careers)/))

Here is another way of doing it:
const urls = ["/about-us", "/memstaff-team", "/careers"];
if (urls.some(url => window.location.href.indexOf(url) > -1)) {
alert("...");
}

Related

Whats the difference between (a==1 || b==1) and ((a || b)==1)

Whats the difference between (a==1 || b==1) and ((a || b)==1)
this code block works right
if(userName==="" || userAge==="" ){
addErrorBool(true);
return;}
but this one not
if((userName || userAge)==="" ){
addErrorBool(true);
return;}
whats does the second one do?
a || b will evaluate to a, if a is truthy, otherwise it'll evaluate to b. So ((a || b)==1) will take whichever value that was and compare it against 1.
For example
(0 || 5) == 1
// equivalent to
(5) == 1
(1 || 2) == 1
// equivalent to
(1) == 1
For what you want, use .some instead, if you want to keep things DRY.
if ([userName, userAge].some(val => val === '')) {
addErrorBool(true);
return;
}
And then you can add as many items to the array .some is called on that you want.
(userName || userAge)==="" means:
(userName || userAge): if userName is truthy, use this value. Otherwise use userAge
==="": compare whichever object was chosen above, and compare that this is a string with no contents.
userName==="" || userAge==="" means:
userName==="": compare userName to see if it is a string with no contents
if it is, the result is true, otherwise:
userAge==="": compare userAge to see if it is a string with no contents
if it is, the result is true, otherwise the result is false

Multiple AND OR operator Javascript

I am wondering if it is correct to write in this way and am I using the brackets correctly?
*This is a code for a country redirects pop-up. The countryCode var is the country of the user (detected by API) while the localStorage.country is which country the user is in on the website.
Feel free to ignore the logic, I just need to know if IF Statement can be written in this way.
if((countryCode == 'sg/' && localStorage.country != "sg/") ||
(countryCode == 'ie/' && localStorage.country != "ie/") ||
(countryCode == 'my/' && localStorage.country != "my/")){
/** Country Redirect Pop Up **/
}
Yes, it is valid and could also be written this way:
let acceptedCountryCodes = ['sg/', 'ie/', 'my/'];
if(countryCode !== localStorage.country && acceptedCountryCodes.includes(countryCode)){
/** Country Redirect Pop Up **/
}
Sure, the code has no problem... It will check if 1st option OR 2nd option OR 3rd option is true
The code currently has no problem!
The wrapping brackets are NOT required in this case because AND(&&) operator has higher precedence than OR(||) operator.
Take a look at the following example:
false && true || false && true = false
The above example is similar to (false && true) || (false && true) which simplies to false || false therefore the final result is false.

Cannot check for an equal number using string

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")

Syntax error when making multiple OR conditionals on jQuery

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
}

Grouping JavaScript if statements of the same 'smaller than'

So I have this long if statement.
if(dupeCheck[0].length > 1 || dupeCheck[1].length > 3 || dupeCheck[2].length > 1 || dupeCheck[3].length > 1){
alert('Copy!');
}
It is possible to group together the dupeCheck[0].length > 1 parts? Like:
if((dupeCheck[0].length||dupeCheck[2].length||dupeCheck[3].length) > 1 || dupeCheck[1].length > 3){
alert('Copy!');
}
Or:
if(dupeCheck[0].length||dupeCheck[2].length||dupeCheck[3].length > 1 || dupeCheck[1].length > 3){
alert('Copy!');
}
I tried both, neither of them worked.
Depending on how many tests you need to run you could also try something like this.
function isGreater(minimum){
for(var i=1;i<arguments.length;i++){
if(arguments[i] > minimum){
return true;
}
}
return false;
}
if(isGreater(1, dupeCheck[0].length, dupeCheck[2].length, dupeCheck[3].length) || isGreater(3, dupeCheck[1].length)){
alert('Copy!');
}
This will let you test any number of values against a number e.g.
if(isGreater(5, 0,0,0,0,2,1,2,3,4) || isGreater(7, 1,3,1,2,1,9,2,1)){
alert('Copy!');
}
Since JavaScript will let you pass as many parameters as you want to a function the 2nd to Nth parameters are tested against the 1st parameter "minimum" and as soon as one passes the test the function returns true. You could adjust the logic to test for other conditions or even ensure that all parameters pass the condition not just one.
There is no built-in language construct to do that.
But what you could do is something like this:
if(Math.min(dupeCheck[0].length, dupeCheck[2].length, dupeCheck[3].length) > 1 || dupeCheck[1].length > 3) {
alert('Copy!');
}

Categories