JavaScript Array verification and finding the last item











up vote
0
down vote

favorite












I want to create a function that accepts an array and returns the last item in the array.



let test=["hichem","amine", 2 , 5];

function getLastItem(arr) {

if (arr.isArray ===true) {

return console.log(arr[arr.lenghth-1]);

}

return console.log("The entered data is not an Array");

}

getLastItem(test);


I write this code and I get this message in the console:




Failed to load resource: net::ERR_FILE_NOT_FOUND




instead of the result










share|improve this question




















  • 3




    Your code has typo. Used lenghth
    – Mohammad
    Nov 13 at 9:23






  • 2




    Why are you returning console.log()? Isn't it strange? lots of errors too...
    – Jai
    Nov 13 at 9:24












  • Except all the mentioned typos in your code, can you show how are including the file??
    – user3559787
    Nov 13 at 9:26










  • FYI: isArray is a static method and should be called as Array.isArray
    – hindmost
    Nov 13 at 9:26












  • I'm learning JS it's kind of practise exercise
    – Hichem Bel Fekih
    Nov 13 at 9:27















up vote
0
down vote

favorite












I want to create a function that accepts an array and returns the last item in the array.



let test=["hichem","amine", 2 , 5];

function getLastItem(arr) {

if (arr.isArray ===true) {

return console.log(arr[arr.lenghth-1]);

}

return console.log("The entered data is not an Array");

}

getLastItem(test);


I write this code and I get this message in the console:




Failed to load resource: net::ERR_FILE_NOT_FOUND




instead of the result










share|improve this question




















  • 3




    Your code has typo. Used lenghth
    – Mohammad
    Nov 13 at 9:23






  • 2




    Why are you returning console.log()? Isn't it strange? lots of errors too...
    – Jai
    Nov 13 at 9:24












  • Except all the mentioned typos in your code, can you show how are including the file??
    – user3559787
    Nov 13 at 9:26










  • FYI: isArray is a static method and should be called as Array.isArray
    – hindmost
    Nov 13 at 9:26












  • I'm learning JS it's kind of practise exercise
    – Hichem Bel Fekih
    Nov 13 at 9:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to create a function that accepts an array and returns the last item in the array.



let test=["hichem","amine", 2 , 5];

function getLastItem(arr) {

if (arr.isArray ===true) {

return console.log(arr[arr.lenghth-1]);

}

return console.log("The entered data is not an Array");

}

getLastItem(test);


I write this code and I get this message in the console:




Failed to load resource: net::ERR_FILE_NOT_FOUND




instead of the result










share|improve this question















I want to create a function that accepts an array and returns the last item in the array.



let test=["hichem","amine", 2 , 5];

function getLastItem(arr) {

if (arr.isArray ===true) {

return console.log(arr[arr.lenghth-1]);

}

return console.log("The entered data is not an Array");

}

getLastItem(test);


I write this code and I get this message in the console:




Failed to load resource: net::ERR_FILE_NOT_FOUND




instead of the result







javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 9:27









Ivan

5,17231339




5,17231339










asked Nov 13 at 9:22









Hichem Bel Fekih

31




31








  • 3




    Your code has typo. Used lenghth
    – Mohammad
    Nov 13 at 9:23






  • 2




    Why are you returning console.log()? Isn't it strange? lots of errors too...
    – Jai
    Nov 13 at 9:24












  • Except all the mentioned typos in your code, can you show how are including the file??
    – user3559787
    Nov 13 at 9:26










  • FYI: isArray is a static method and should be called as Array.isArray
    – hindmost
    Nov 13 at 9:26












  • I'm learning JS it's kind of practise exercise
    – Hichem Bel Fekih
    Nov 13 at 9:27














  • 3




    Your code has typo. Used lenghth
    – Mohammad
    Nov 13 at 9:23






  • 2




    Why are you returning console.log()? Isn't it strange? lots of errors too...
    – Jai
    Nov 13 at 9:24












  • Except all the mentioned typos in your code, can you show how are including the file??
    – user3559787
    Nov 13 at 9:26










  • FYI: isArray is a static method and should be called as Array.isArray
    – hindmost
    Nov 13 at 9:26












  • I'm learning JS it's kind of practise exercise
    – Hichem Bel Fekih
    Nov 13 at 9:27








3




3




Your code has typo. Used lenghth
– Mohammad
Nov 13 at 9:23




Your code has typo. Used lenghth
– Mohammad
Nov 13 at 9:23




2




2




Why are you returning console.log()? Isn't it strange? lots of errors too...
– Jai
Nov 13 at 9:24






Why are you returning console.log()? Isn't it strange? lots of errors too...
– Jai
Nov 13 at 9:24














Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26




Except all the mentioned typos in your code, can you show how are including the file??
– user3559787
Nov 13 at 9:26












FYI: isArray is a static method and should be called as Array.isArray
– hindmost
Nov 13 at 9:26






FYI: isArray is a static method and should be called as Array.isArray
– hindmost
Nov 13 at 9:26














I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27




I'm learning JS it's kind of practise exercise
– Hichem Bel Fekih
Nov 13 at 9:27












4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










Try this



function getLastItem() {
var arr=["hichem","amine", 2 , 5];
if (arr instanceof Array){
return arr[arr.length-1];
}
}





share|improve this answer




























    up vote
    2
    down vote













    If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.



    You have used the wrong syntax.
    It is Array.isArray(arr).



    let test=["hichem","amine", 2 , 5];

    function getLastItem(arr) {
    if (Array.isArray(arr)) {
    return (arr[arr.length-1]);
    }
    return "The entered data is not an Array";
    }

    console.log(getLastItem(test));





    share|improve this answer



















    • 1




      It can be just written Array.isArray(arr).
      – chŝdk
      Nov 13 at 9:40










    • It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
      – hindmost
      Nov 13 at 9:47










    • @hindmost Thanks for the suggestion! Now the answer will give output
      – Vijay Pushkin
      Nov 13 at 9:51


















    up vote
    0
    down vote













    All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.



    let test=["hichem","amine", 2 , 5];
    function getLastItem(arr) {
    var isArray=(arr instanceof Array); //or test.constructor === Array
    if (isArray) {
    return (arr[arr.length-1]);
    }
    return console.log("The entered data is not an Array");
    }
    getLastItem(test);





    share|improve this answer




























      up vote
      0
      down vote














      • arr.isArray is undefine, you can coding like this:





      let test=["hichem","amine", 2 , 5];

      function getLastItem(arr) {

      if (arr instanceof Array) {

      return console.log(arr[arr.lenghth-1]);

      }

      return console.log("The entered data is not an Array");

      }

      getLastItem(test);








      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',
        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%2f53277646%2fjavascript-array-verification-and-finding-the-last-item%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote



        accepted










        Try this



        function getLastItem() {
        var arr=["hichem","amine", 2 , 5];
        if (arr instanceof Array){
        return arr[arr.length-1];
        }
        }





        share|improve this answer

























          up vote
          0
          down vote



          accepted










          Try this



          function getLastItem() {
          var arr=["hichem","amine", 2 , 5];
          if (arr instanceof Array){
          return arr[arr.length-1];
          }
          }





          share|improve this answer























            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            Try this



            function getLastItem() {
            var arr=["hichem","amine", 2 , 5];
            if (arr instanceof Array){
            return arr[arr.length-1];
            }
            }





            share|improve this answer












            Try this



            function getLastItem() {
            var arr=["hichem","amine", 2 , 5];
            if (arr instanceof Array){
            return arr[arr.length-1];
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 at 9:38









            Prakash N

            347




            347
























                up vote
                2
                down vote













                If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.



                You have used the wrong syntax.
                It is Array.isArray(arr).



                let test=["hichem","amine", 2 , 5];

                function getLastItem(arr) {
                if (Array.isArray(arr)) {
                return (arr[arr.length-1]);
                }
                return "The entered data is not an Array";
                }

                console.log(getLastItem(test));





                share|improve this answer



















                • 1




                  It can be just written Array.isArray(arr).
                  – chŝdk
                  Nov 13 at 9:40










                • It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                  – hindmost
                  Nov 13 at 9:47










                • @hindmost Thanks for the suggestion! Now the answer will give output
                  – Vijay Pushkin
                  Nov 13 at 9:51















                up vote
                2
                down vote













                If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.



                You have used the wrong syntax.
                It is Array.isArray(arr).



                let test=["hichem","amine", 2 , 5];

                function getLastItem(arr) {
                if (Array.isArray(arr)) {
                return (arr[arr.length-1]);
                }
                return "The entered data is not an Array";
                }

                console.log(getLastItem(test));





                share|improve this answer



















                • 1




                  It can be just written Array.isArray(arr).
                  – chŝdk
                  Nov 13 at 9:40










                • It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                  – hindmost
                  Nov 13 at 9:47










                • @hindmost Thanks for the suggestion! Now the answer will give output
                  – Vijay Pushkin
                  Nov 13 at 9:51













                up vote
                2
                down vote










                up vote
                2
                down vote









                If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.



                You have used the wrong syntax.
                It is Array.isArray(arr).



                let test=["hichem","amine", 2 , 5];

                function getLastItem(arr) {
                if (Array.isArray(arr)) {
                return (arr[arr.length-1]);
                }
                return "The entered data is not an Array";
                }

                console.log(getLastItem(test));





                share|improve this answer














                If I understand your question correctly, you want to return the last item if it's an array or give a console log statement if it's not.



                You have used the wrong syntax.
                It is Array.isArray(arr).



                let test=["hichem","amine", 2 , 5];

                function getLastItem(arr) {
                if (Array.isArray(arr)) {
                return (arr[arr.length-1]);
                }
                return "The entered data is not an Array";
                }

                console.log(getLastItem(test));






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 13 at 9:50

























                answered Nov 13 at 9:32









                Vijay Pushkin

                714




                714








                • 1




                  It can be just written Array.isArray(arr).
                  – chŝdk
                  Nov 13 at 9:40










                • It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                  – hindmost
                  Nov 13 at 9:47










                • @hindmost Thanks for the suggestion! Now the answer will give output
                  – Vijay Pushkin
                  Nov 13 at 9:51














                • 1




                  It can be just written Array.isArray(arr).
                  – chŝdk
                  Nov 13 at 9:40










                • It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                  – hindmost
                  Nov 13 at 9:47










                • @hindmost Thanks for the suggestion! Now the answer will give output
                  – Vijay Pushkin
                  Nov 13 at 9:51








                1




                1




                It can be just written Array.isArray(arr).
                – chŝdk
                Nov 13 at 9:40




                It can be just written Array.isArray(arr).
                – chŝdk
                Nov 13 at 9:40












                It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                – hindmost
                Nov 13 at 9:47




                It'd be better to move console.log out of the function and wrap the function's result. Currently this code doesn't output anything if the function receives an array.
                – hindmost
                Nov 13 at 9:47












                @hindmost Thanks for the suggestion! Now the answer will give output
                – Vijay Pushkin
                Nov 13 at 9:51




                @hindmost Thanks for the suggestion! Now the answer will give output
                – Vijay Pushkin
                Nov 13 at 9:51










                up vote
                0
                down vote













                All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.



                let test=["hichem","amine", 2 , 5];
                function getLastItem(arr) {
                var isArray=(arr instanceof Array); //or test.constructor === Array
                if (isArray) {
                return (arr[arr.length-1]);
                }
                return console.log("The entered data is not an Array");
                }
                getLastItem(test);





                share|improve this answer

























                  up vote
                  0
                  down vote













                  All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.



                  let test=["hichem","amine", 2 , 5];
                  function getLastItem(arr) {
                  var isArray=(arr instanceof Array); //or test.constructor === Array
                  if (isArray) {
                  return (arr[arr.length-1]);
                  }
                  return console.log("The entered data is not an Array");
                  }
                  getLastItem(test);





                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.



                    let test=["hichem","amine", 2 , 5];
                    function getLastItem(arr) {
                    var isArray=(arr instanceof Array); //or test.constructor === Array
                    if (isArray) {
                    return (arr[arr.length-1]);
                    }
                    return console.log("The entered data is not an Array");
                    }
                    getLastItem(test);





                    share|improve this answer












                    All is good in your function but "arr.isArray ===true" this line is not working. it should be something like below code. But i don't know why you are returning console.log from a function. but if you want forcefully there is nothing wrong just modify your code.



                    let test=["hichem","amine", 2 , 5];
                    function getLastItem(arr) {
                    var isArray=(arr instanceof Array); //or test.constructor === Array
                    if (isArray) {
                    return (arr[arr.length-1]);
                    }
                    return console.log("The entered data is not an Array");
                    }
                    getLastItem(test);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 13 at 9:34









                    Negi Rox

                    1,5271511




                    1,5271511






















                        up vote
                        0
                        down vote














                        • arr.isArray is undefine, you can coding like this:





                        let test=["hichem","amine", 2 , 5];

                        function getLastItem(arr) {

                        if (arr instanceof Array) {

                        return console.log(arr[arr.lenghth-1]);

                        }

                        return console.log("The entered data is not an Array");

                        }

                        getLastItem(test);








                        share|improve this answer

























                          up vote
                          0
                          down vote














                          • arr.isArray is undefine, you can coding like this:





                          let test=["hichem","amine", 2 , 5];

                          function getLastItem(arr) {

                          if (arr instanceof Array) {

                          return console.log(arr[arr.lenghth-1]);

                          }

                          return console.log("The entered data is not an Array");

                          }

                          getLastItem(test);








                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote










                            • arr.isArray is undefine, you can coding like this:





                            let test=["hichem","amine", 2 , 5];

                            function getLastItem(arr) {

                            if (arr instanceof Array) {

                            return console.log(arr[arr.lenghth-1]);

                            }

                            return console.log("The entered data is not an Array");

                            }

                            getLastItem(test);








                            share|improve this answer













                            • arr.isArray is undefine, you can coding like this:





                            let test=["hichem","amine", 2 , 5];

                            function getLastItem(arr) {

                            if (arr instanceof Array) {

                            return console.log(arr[arr.lenghth-1]);

                            }

                            return console.log("The entered data is not an Array");

                            }

                            getLastItem(test);








                            let test=["hichem","amine", 2 , 5];

                            function getLastItem(arr) {

                            if (arr instanceof Array) {

                            return console.log(arr[arr.lenghth-1]);

                            }

                            return console.log("The entered data is not an Array");

                            }

                            getLastItem(test);





                            let test=["hichem","amine", 2 , 5];

                            function getLastItem(arr) {

                            if (arr instanceof Array) {

                            return console.log(arr[arr.lenghth-1]);

                            }

                            return console.log("The entered data is not an Array");

                            }

                            getLastItem(test);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 13 at 9:47









                            bolt

                            782




                            782






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277646%2fjavascript-array-verification-and-finding-the-last-item%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

                                How to change which sound is reproduced for terminal bell?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?