Javascript some() returns false on zero
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to check if numbers exist in my array. Using the some() function I find that zero comes back false. It's a problem because I am working with a ton of different numbers and zero being one of them.
var array = [0, 1, 3, 4, 5];
var test = function(element) {
return 0;
};
console.log(array.some(test));
// expected output: true on 0 <-- getting false
// expected output: true on 1
// expected output: false on 20
In short how can I get 0 to return true?
javascript
add a comment |
I need to check if numbers exist in my array. Using the some() function I find that zero comes back false. It's a problem because I am working with a ton of different numbers and zero being one of them.
var array = [0, 1, 3, 4, 5];
var test = function(element) {
return 0;
};
console.log(array.some(test));
// expected output: true on 0 <-- getting false
// expected output: true on 1
// expected output: false on 20
In short how can I get 0 to return true?
javascript
3
Your function always only returns0
, a falsey value. The function is supposed to do whatever comparison you want, and returntrue
orfalse
!
– deceze♦
Nov 17 '18 at 7:18
add a comment |
I need to check if numbers exist in my array. Using the some() function I find that zero comes back false. It's a problem because I am working with a ton of different numbers and zero being one of them.
var array = [0, 1, 3, 4, 5];
var test = function(element) {
return 0;
};
console.log(array.some(test));
// expected output: true on 0 <-- getting false
// expected output: true on 1
// expected output: false on 20
In short how can I get 0 to return true?
javascript
I need to check if numbers exist in my array. Using the some() function I find that zero comes back false. It's a problem because I am working with a ton of different numbers and zero being one of them.
var array = [0, 1, 3, 4, 5];
var test = function(element) {
return 0;
};
console.log(array.some(test));
// expected output: true on 0 <-- getting false
// expected output: true on 1
// expected output: false on 20
In short how can I get 0 to return true?
javascript
javascript
asked Nov 17 '18 at 7:16
limitlimit
352419
352419
3
Your function always only returns0
, a falsey value. The function is supposed to do whatever comparison you want, and returntrue
orfalse
!
– deceze♦
Nov 17 '18 at 7:18
add a comment |
3
Your function always only returns0
, a falsey value. The function is supposed to do whatever comparison you want, and returntrue
orfalse
!
– deceze♦
Nov 17 '18 at 7:18
3
3
Your function always only returns
0
, a falsey value. The function is supposed to do whatever comparison you want, and return true
or false
!– deceze♦
Nov 17 '18 at 7:18
Your function always only returns
0
, a falsey value. The function is supposed to do whatever comparison you want, and return true
or false
!– deceze♦
Nov 17 '18 at 7:18
add a comment |
2 Answers
2
active
oldest
votes
the test function always returns zero.
var test = function(element) {
return element == 0
};
This way the function should work properly.
The test function should return true/false.
In your case you always return 0 which is evaluated to boolean false.
use strict comparison operator, nlelement === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
add a comment |
For what you're trying to implement, it might make more sense to use the .includes
function, which tests whether an array includes a value:
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
Though, .includes
(along with all array iteration methods) is an O(N)
process - if you need to carry out a bunch of tests for the same array, you might convert the array to a Set
first, so that you can then use Set.has
(which is generally O(1)
):
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349089%2fjavascript-some-returns-false-on-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
the test function always returns zero.
var test = function(element) {
return element == 0
};
This way the function should work properly.
The test function should return true/false.
In your case you always return 0 which is evaluated to boolean false.
use strict comparison operator, nlelement === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
add a comment |
the test function always returns zero.
var test = function(element) {
return element == 0
};
This way the function should work properly.
The test function should return true/false.
In your case you always return 0 which is evaluated to boolean false.
use strict comparison operator, nlelement === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
add a comment |
the test function always returns zero.
var test = function(element) {
return element == 0
};
This way the function should work properly.
The test function should return true/false.
In your case you always return 0 which is evaluated to boolean false.
the test function always returns zero.
var test = function(element) {
return element == 0
};
This way the function should work properly.
The test function should return true/false.
In your case you always return 0 which is evaluated to boolean false.
answered Nov 17 '18 at 7:17
user2693928user2693928
1,9211716
1,9211716
use strict comparison operator, nlelement === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
add a comment |
use strict comparison operator, nlelement === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
use strict comparison operator, nl
element === 0
– Icepickle
Nov 17 '18 at 7:19
use strict comparison operator, nl
element === 0
– Icepickle
Nov 17 '18 at 7:19
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
Yes, strict comparison is better thx
– user2693928
Nov 17 '18 at 7:20
add a comment |
For what you're trying to implement, it might make more sense to use the .includes
function, which tests whether an array includes a value:
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
Though, .includes
(along with all array iteration methods) is an O(N)
process - if you need to carry out a bunch of tests for the same array, you might convert the array to a Set
first, so that you can then use Set.has
(which is generally O(1)
):
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
add a comment |
For what you're trying to implement, it might make more sense to use the .includes
function, which tests whether an array includes a value:
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
Though, .includes
(along with all array iteration methods) is an O(N)
process - if you need to carry out a bunch of tests for the same array, you might convert the array to a Set
first, so that you can then use Set.has
(which is generally O(1)
):
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
add a comment |
For what you're trying to implement, it might make more sense to use the .includes
function, which tests whether an array includes a value:
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
Though, .includes
(along with all array iteration methods) is an O(N)
process - if you need to carry out a bunch of tests for the same array, you might convert the array to a Set
first, so that you can then use Set.has
(which is generally O(1)
):
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
For what you're trying to implement, it might make more sense to use the .includes
function, which tests whether an array includes a value:
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
Though, .includes
(along with all array iteration methods) is an O(N)
process - if you need to carry out a bunch of tests for the same array, you might convert the array to a Set
first, so that you can then use Set.has
(which is generally O(1)
):
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
var array = [0, 1, 3, 4, 5];
console.log(array.includes(0));
console.log(array.includes(1));
console.log(array.includes(2));
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
var array = [0, 1, 3, 4, 5];
const set = new Set(array);
console.log(set.has(0));
console.log(set.has(1));
console.log(set.has(2));
answered Nov 17 '18 at 7:19
CertainPerformanceCertainPerformance
101k166393
101k166393
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53349089%2fjavascript-some-returns-false-on-zero%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Your function always only returns
0
, a falsey value. The function is supposed to do whatever comparison you want, and returntrue
orfalse
!– deceze♦
Nov 17 '18 at 7:18