Equality operator with multiple values in JavaScript - javascript

Is there a simple way to compare multiple values to a single value in Javascript ?
Like, instead of writing :
if (k != 2 && k != 3 && k!= 7 && k!12)
Writing something like :
if (k != {2,3,7,12})

You can use includes instead of multiple equality comparisons.
if (![2,3,7,12].includes(k))
That's boolean algebra:
if (k != 2 && k != 3 && k!= 7 && k != 12)
is equivalent to
if (!(k == 2 || k == 3 || k == 7 || k == 12))
and that's equivalent to
if (![2,3,7,12].includes(k))

Related

javascript, combining AND, OR in an if starement

I would like my code to be able to do the following:
IF (value1 is less than -1 AND variable2 does not equal either 6 or 5) then...
So far I could get a single if and to work as follows;
if ( (value < -1 && (day !== 6)) ) sendEmail(value)
but as soon as I add in the second or it falls over. What am I doing wrong? I have tried it in a few different ways but below are a couple of examples that did not work.
if ( (value < -1 && (day !== 6 || 5)) ) sendEmail(value)
if ( (value < -1 && (day !== 6 || day !== 5)) ) sendEmail(value)
Think about your logic. Basically what you want is:
IF (value1 is less than -1 AND variable2 is not equal to 6 AND variable2 is not equal to 5)
Therefore your if statement can be written as:
if (value < -1 && day != 6 && day != 5) sendEmail(value);
if(value < -1 && day !== 6 && day !== 5) sendEmail(value)
should do the trick.

Why does javascript accept a decimal as an integer

I have this html:
<input type='number' id='length' step='0.1' min='0'; max='5'>Length
and this Javascript
num=document.getElementById('length').value;
if(num==1 || 2 || 3 || 4|| 5){
num='0'+num;
}
My problem is this: while I only want the code inside the brackets to execute if the number from the input is an integer, it also activates if it detects 0.8 or some other decimal. Any Idea why? How do I fix it? Thanks.
To make sure num is a whole number, without having to define all possibilities, use:
if (num % 1 == 0)
Why:
num==1 || 2 || 3 || 4|| 5
equals to:
(num==1) || 2 || 3 || 4|| 5
so if num is "1" (always a string type), the expression returns true, otherwise 2 (also a truthy value), eventually your if statement always succeeds.
How to fix:
// implicitly converts the string type into number type before comparison
// returns true if it is an integer-like string
num == Math.floor(num)
So you could do it like this:
if (num == Math.floor(num) && num > 0 && num < 6) {
// an integer-like string that meets the requirement [1, 5]
}
But remember, the num is still string type now. If you want a number, do:
num = +num
You should do
if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)
WRONG - otherwise it will compare 2 with 2 and says it's true for the 4 last 'if' parameters.
CORRECTO - any number in JS is considered as true.
You have to edit the "If" loop:
if (num == 1 || num == 2 || num == 3 || num == 4 || num == 5)

How can I loop through two arrays comparing values in Javascript?

I have the following Javascript function where the parameters newValue and oldValue are arrays of integers and the same length. Any values in these arrays can be an integer, undefined or null:
function (newValue, oldValue) {
});
Is there some way that I could check the values in the arrays one element at a time and then do an action only if:
newValue[index] is >= 0 and < 999
oldValue[index] is >= 0 and < 999
newValue[index] is not equal to oldValue[index]
What I am not sure of is how can I handle in my checks and ignore the cases where newValue or oldValue are not null and not undefined? I know I can do a check as in if (newValue) but then this will show false when it's a 0.
Update:
I had a few quick answers so far but none are checking the right things which I listed above.
compare against null and undefined:
if (newValue[index] !== null && typeof newValue[index] !== 'undefined') {}
for OPs update:
n = newValue[index];
o = oldValue[index];
if (
n !== null && typeof n !== 'undefined' && n >= 0 && n < 999 &&
o !== null && typeof o !== 'undefined' && o >= 0 && o < 999
) {
// your code
}
for array-elements its not necessary to use typeof so n !== undefined is ok because the variable will exist.
n = newValue[index];
o = oldValue[index];
if (
n !== null && n !== undefined && n >= 0 && n < 999 &&
o !== null && o !== undefined && o >= 0 && o < 999 &&
n !== o
) {
// your code
}
This will do it:
function isEqual (newValue, oldValue) {
for (var i=0, l=newValue.length; i<l; i++) {
if (newValue[i] == null || newValue[i] < 0 || newValue[i] >= 999
|| oldValue[i] == null || oldValue[i] < 0 || oldValue[i] >= 999)
continue;
if (newVale[i] !== oldValue[i])
return false;
}
return true;
}
if (newValue != null || newValue != undefined) && (oldValue != null || oldValue != undefined)

Location.pathname.indexOf not working with 'Or' ( || )

I'm trying to use location.pathname.indexOf to make conditional jQuery work on some pages on my site.
This works:
if (location.pathname.indexOf("/example/5820.htm") != 0){}
This works:
if (location.pathname.indexOf("/example-1/3569.htm") != 0) {}
This doesn't work:
if (location.pathname.indexOf("/example/5820.htm") != 0 || location.pathname.indexOf("/example-1/3569.htm") != 0) {}
I've done this a ton of times and for some reason this code is not working. I'm wondering if I'm missing something little in the code or if it's something else?
Tim already answered this question, but don't forget:
.indexOf() will return -1 when the string isn't found, not 0.
if (location.pathname.indexOf("/example/5820.htm") != 0){}
Should be:
if (location.pathname.indexOf("/example/5820.htm") != -1){}
Or:
if (location.pathname.indexOf("/example/5820.htm") >= 0){}
http://www.w3schools.com/jsref/jsref_indexof.asp
basically you're saying this:
var a = 0;
var b = 1;
if (a != 0 || b != 0) {};
Which is equal to
if (!(a == 0 && b == 0)) {};
However, you actually want this:
if (!(a == 0 || b == 0)) {};
Which is equal to:
if (a != 0 && b != 0) {};

jQuery KeyFilter Plugin Problem

Using keyfilter plugin for jQuery, and all seems to be fine apart from one problem.
I am using a regular expression to filter the element.
$('#nameVal').keyfilter(/[0-9a-zA-Z]/);
The odd thing is, this is not only allowing alpha-numeric characters but it is also allowing '(' to be entered. In fact, it doesn't matter what expression I pass to the keyfilter, I can't stop '(' being allowed in the textbox.
Has anyone else experienced this problem and is there a solution?
I found the problem.
The isSpecialKey function in the KeyFilter plugin returns true if the keycode is 40, which is the keycode for '('. This means that the test is never performed on the character.
var isSpecialKey = function(e)
{
var k = e.keyCode;
var c = e.charCode;
return k == 9 || k == 13 || /*(k == 40 && (!$.browser.opera || !e.shiftKey)) ||*/ k == 27 ||
k == 16 || k == 17 ||
(k >= 18 && k <= 20) ||
($.browser.opera && !e.shiftKey && (k == 8 || (k >= 33 && k <= 35) || (k >= 36 && k <= 39) || (k >= 44 && k <= 45)))
;
};
Commenting out the bit of code as I have above fixes the problem. Not entirely sure what effect this will have in other browsers, but it works in IE8 and Chrome.

Categories