Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to do this
if ($(this).val() in (1,2,3) ) {
...
}
I have tried this and it works but It's not what I want
if ($(this).val()==1 || $(this).val()==2 || $(this).val()==3 ) {
...
}
I notice OP is using jQuery but this should work for anyone who isn't
if([1,2,3].indexOf(yourValue) > -1) {
...
}
Edit, Gah, beaten by Blazemonger by 20 seconds
Use an array and $.inArray():
if ($.inArray(parseInt($(this).val()), [1, 2, 3]) > -1) {
}
Attempting to synthesize information that is currently scattered in a number of different answers and comments... Your code is not working because of two things:
You are using in which checks for keys, not values in an array. Thus, for example, 3 in [1,2,3] will be false, since [1,2,3] has keys (indices) 0,1,2.
Quite possibly, your value is a string
The solution is to make sure that your value is of the right type, and to use the correct function. There are two obvious choices:
indexOf - returns the index of the (first) element that matches
inArray - is available if you are using jQuery
Sample code:
var v = parseInt($(this).val()); // radix 10
var r = [1,2,3]; // the values to check against
if(r.indexOf(v) > -1) { // method 1
if($.inArray(v, r)) { // method 2
if ([1,2,3].indexOf($(this).val()) > -1) {
or
for (i in [1,2,3])
if (i==$(this).val()) {
...
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
const allEvens = (arr) =>allEvens.every(item => return item %2 ===0)
}
Hey, so im trying here to check if every number is even, and if its not it should return false. But now Ive spent almost 2 hours trying to understand why this doesnt work for me, trying to change some things but none of them worked. Can someone give me any clue where's the problem here ? Thank you very much!
You have to remove the return keyword to make it return (item % 2 === 0) expression. But if you want to keep that return, you have to put the body of function inside curly braces.
When you omit "{...}" from the body of an arrow function, it automatically indicates that you want to directly return the content (the result of item % 2 === 0 in this case). So the following should help solve the issue:
const allEvens = (arr) => allEvens.every((item) => item % 2 === 0);
console.log(allEvens([12, 6, 8])) // true
console.log(allEvens([12, 6, 8, 1])) // false
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
handleChange(e) {
const index = Number(e.target.id.substring(e.target.id.length - 1, 19));
const copyFormArray = JSON.parse(JSON.stringify(this.state.educationData));
copyFormArray[index][e.target.name] = e.target.value;
this.setState({
educationData: copyFormArray,
});
};
What does this handleChange method do, specifically in the index variable? I understand what substring and the Number constructor do but the second argument I cannot find any answers for this specific question although it works as intended.
Substring works in a tricky way, if second argument (in your case 19) is less than first argument - then they will be "swapped", and if not - it will just take text between indexes (argument1 and argument2).
So say for example i have a string:
var x = "aaaaa-5";
if i will write:
x.substring(x.length-1)
It will give me "5".
Then if i will write:
x.substring(3,6)
It will give me "aa-"
But then if i will write:
x.substring(6,3)
It will be exactly the same as previous call - "aa-"
So internally logic of substring is kind of like this:
function substring(start,end) {
if(start > end) {
var buf = end;
end = start;
start = buf;
}
//... other logic
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have provided binary for example, 01001, and would like to get positions of bit 1, so I expect it will return me [0, 3].
Is there any function provided by javascript to get all positions of bit 1?
If it's a number, convert it to a string before hand, if it is a string you can do:
positionArray = [];
binary.split('');
for (i = 0; i < binary.length; i++){
if (binary[i] == "1"){
positionArray.push(i);
}
}
return positionArray;
What i'm essentially doing is converting the string to an array (or number to a string to an array) of characters and then going through each entry to find the positions of each '1'.
Not sure you can do it this way with numbers, which is why I suggested converting it to a string before hand. It's just a rough solution and I bet there are much better ways of doing it but hope it helps.
MDN has a useful piece of code that works numerically and will take a number like 9 or in binary 0b1001 and return you an array with true/false values depending on whether or not the corresponding bit is set in the input value. For 1001 it returns [true, false, false, true]
You can then use this array to create your desired output by checking which indexes are true, and getting [0,3]
The MDN snippet is here:
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Reverse_algorithm_an_array_of_booleans_from_a_mask
And a quick example of using it is here:
https://repl.it/DuEh/5
#DibsyJr has a nice solution if you are dealing with strings. However if you start from a number, then converting it to a string is an unnecessary overhead. You can do it efficiently with this function:
function get_idxs(x) {
var arr = [];
var idx = 0;
while (x) {
if (x & 1) {
arr.push(idx);
}
idx++;
x >>= 1;
}
return arr;
}
> get_idxs(0b1001);
[0, 3]
WARNING: It does not work for negative numbers. Unfortunately for negative numbers the answer to the question depends on the underlying cpu architecture.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The book that I'm studying says about iterating over arrays with every that:
The function these methods use must follow one ruleāit must accept
three arguments like the following code:
function functionName(value, index, array) {
// do something here
}
Does that mean that I must always use 3 arguments? If so then why does this code work?
var numbers = [ 1, 2, 2 ];
function isLessThan3(value) {
var returnValue = false;
if (value < 3) {
returnValue = true;
}
return returnValue; }
document.write(numbers.every(isLessThan3));
There is no limitation on how many arrguments you can put in a function with Javascript.
you have a very good explenation about this topic in the next answer by #Niet the Dark Absol
https://stackoverflow.com/a/22747272/1283672
i believe that the book was reffering to something more specific within it's scope.
And just to be clear you can put no arrgs in a function either.
It's a bit ugly, the code, you have, but there is help. You might use the following without a temporary variable. Just return the result of the comparison.
function allLessThan3(value) {
return value < 3;
}
var numbers = [1, 2, 2];
console.log(numbers.every(allLessThan3));
No, you can use from 0 to 3 arguments
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I don't know what is better to use:
var s = "2";
var i = 2;
if(s.toString() === i.toString()){
//do something
}
//OR
if(s == i){
//do something
}
Thanks for the help
You are actually comparing two separate things, in first, you are casting both the variable values to a string and comparing, and the other comparison is lose one i.e you are not actually checking the data types of those variables. so it will return true if you compare string with int with a same value.
According to me, what you should be using is === which will not only compare the values but their data types as well because the ones you are using are both considered lose.
If you do not consider at all about data type then using == will suffice. You don't have to cast the values to a string.
In your first example if for any reason you get a 2 with a space, it will evaluate false (even with ==):
var s = " 2"; // 2 with a sneaky space
var i = 2;
if(s.toString() === i.toString()){ // will be false
//do something
}
Personally I prefer using ===, but I would change the values to integers, instead of to strings.
var s = " 2"; // 2 with a sneaky space again
var i = 2;
if(Number(s) === Number(i)){ // will be true
//do something
}
You don't need the second Number() but, I don't know, you may get data that is also a string.