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;
}







-2















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?










share|improve this question


















  • 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


















-2















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?










share|improve this question


















  • 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














-2












-2








-2








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 17 '18 at 7:16









limitlimit

352419




352419








  • 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














  • 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








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












2 Answers
2






active

oldest

votes


















4














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.






share|improve this answer
























  • 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



















3














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








share|improve this answer
























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    4














    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.






    share|improve this answer
























    • 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
















    4














    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.






    share|improve this answer
























    • 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














    4












    4








    4







    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.






    share|improve this answer













    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 17 '18 at 7:17









    user2693928user2693928

    1,9211716




    1,9211716













    • 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



















    • 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

















    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













    3














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








    share|improve this answer




























      3














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








      share|improve this answer


























        3












        3








        3







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








        share|improve this answer













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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 17 '18 at 7:19









        CertainPerformanceCertainPerformance

        101k166393




        101k166393






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python