How to use a for loop and splice to remove a word and then check an array for a specific word





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.



    var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]

function removeElement (aTable, aName) {

for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {

aTable.splice(i, 1)
document.write(aTable); {break;}

} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}


I've tried to solve it this way, but I don't get it right. The output should be something like this:



Anne, Kari, Marit, Ingrid
Victoria is not in the list









share|improve this question

























  • why is break wrapped in a block?

    – Nina Scholz
    Nov 16 '18 at 20:50











  • I am missing where you assign the array femaleName to your aTable

    – ratmalwer
    Nov 16 '18 at 20:59


















0















I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.



    var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]

function removeElement (aTable, aName) {

for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {

aTable.splice(i, 1)
document.write(aTable); {break;}

} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}


I've tried to solve it this way, but I don't get it right. The output should be something like this:



Anne, Kari, Marit, Ingrid
Victoria is not in the list









share|improve this question

























  • why is break wrapped in a block?

    – Nina Scholz
    Nov 16 '18 at 20:50











  • I am missing where you assign the array femaleName to your aTable

    – ratmalwer
    Nov 16 '18 at 20:59














0












0








0








I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.



    var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]

function removeElement (aTable, aName) {

for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {

aTable.splice(i, 1)
document.write(aTable); {break;}

} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}


I've tried to solve it this way, but I don't get it right. The output should be something like this:



Anne, Kari, Marit, Ingrid
Victoria is not in the list









share|improve this question
















I want to make a function that looks for a specific name (Inger) in an array, and the removes that name. Then I want the function to tell that a name doesn't exist in the array.



    var femaleName = ["Anne","Inger","Kari","Marit","Ingrid"]

function removeElement (aTable, aName) {

for (var i = 0; i <= aTable.length - 1; i++) {
if (aTable[1] === aName) {

aTable.splice(i, 1)
document.write(aTable); {break;}

} else if (aTable[i] !== aName) {
document.write(aName + " is not in the list");
}
}
}


I've tried to solve it this way, but I don't get it right. The output should be something like this:



Anne, Kari, Marit, Ingrid
Victoria is not in the list






javascript jquery html arrays string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 1:27









Jack Bashford

17.1k51849




17.1k51849










asked Nov 16 '18 at 20:47









SenselessSenseless

427




427













  • why is break wrapped in a block?

    – Nina Scholz
    Nov 16 '18 at 20:50











  • I am missing where you assign the array femaleName to your aTable

    – ratmalwer
    Nov 16 '18 at 20:59



















  • why is break wrapped in a block?

    – Nina Scholz
    Nov 16 '18 at 20:50











  • I am missing where you assign the array femaleName to your aTable

    – ratmalwer
    Nov 16 '18 at 20:59

















why is break wrapped in a block?

– Nina Scholz
Nov 16 '18 at 20:50





why is break wrapped in a block?

– Nina Scholz
Nov 16 '18 at 20:50













I am missing where you assign the array femaleName to your aTable

– ratmalwer
Nov 16 '18 at 20:59





I am missing where you assign the array femaleName to your aTable

– ratmalwer
Nov 16 '18 at 20:59












4 Answers
4






active

oldest

votes


















4














Do you have to write functions? Javascript has Array methods to do this for you.



Array.prototype.filter()




The filter() method creates a new array with all elements that pass the test implemented by the provided function.




includes()




The includes() method determines whether an array includes a certain element, returning true or false as appropriate.







var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

femaleName = femaleName.filter(name => name !== 'Inger')

console.log(femaleName);

console.log(femaleName.includes('Inger'));








share|improve this answer































    2














    The issue is this line:



    if (aTable[1] === aName) {


    That only checks the second index ("Inger"). It should be this:



    if (aTable[i] === aName) {





    share|improve this answer































      2














      You could do this:



      function removeElement(aTable, aName) {
      const index = aTable.indexOf(aName);
      if (index > -1) {
      aTable.splice(index, 1);
      document.write(aTable);
      } else {
      document.write(aName + " is not in the list");
      }
      }


      Or you could always use functional Array.prototype.filter().






      share|improve this answer

































        1














        Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result






        const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

        function notify(criteria) {
        const position = getPosition(criteria);
        const removed = removeName(position);

        console.log(names);
        console.log(`${removed} is not in the list`);
        }

        function getPosition(criteria) {
        return names.findIndex(name => name === criteria);
        }

        function removeName(position) {
        return names.splice(position, 1);
        }

        notify('Inger');








        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%2f53345163%2fhow-to-use-a-for-loop-and-splice-to-remove-a-word-and-then-check-an-array-for-a%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









          4














          Do you have to write functions? Javascript has Array methods to do this for you.



          Array.prototype.filter()




          The filter() method creates a new array with all elements that pass the test implemented by the provided function.




          includes()




          The includes() method determines whether an array includes a certain element, returning true or false as appropriate.







          var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

          femaleName = femaleName.filter(name => name !== 'Inger')

          console.log(femaleName);

          console.log(femaleName.includes('Inger'));








          share|improve this answer




























            4














            Do you have to write functions? Javascript has Array methods to do this for you.



            Array.prototype.filter()




            The filter() method creates a new array with all elements that pass the test implemented by the provided function.




            includes()




            The includes() method determines whether an array includes a certain element, returning true or false as appropriate.







            var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

            femaleName = femaleName.filter(name => name !== 'Inger')

            console.log(femaleName);

            console.log(femaleName.includes('Inger'));








            share|improve this answer


























              4












              4








              4







              Do you have to write functions? Javascript has Array methods to do this for you.



              Array.prototype.filter()




              The filter() method creates a new array with all elements that pass the test implemented by the provided function.




              includes()




              The includes() method determines whether an array includes a certain element, returning true or false as appropriate.







              var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

              femaleName = femaleName.filter(name => name !== 'Inger')

              console.log(femaleName);

              console.log(femaleName.includes('Inger'));








              share|improve this answer













              Do you have to write functions? Javascript has Array methods to do this for you.



              Array.prototype.filter()




              The filter() method creates a new array with all elements that pass the test implemented by the provided function.




              includes()




              The includes() method determines whether an array includes a certain element, returning true or false as appropriate.







              var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

              femaleName = femaleName.filter(name => name !== 'Inger')

              console.log(femaleName);

              console.log(femaleName.includes('Inger'));








              var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

              femaleName = femaleName.filter(name => name !== 'Inger')

              console.log(femaleName);

              console.log(femaleName.includes('Inger'));





              var femaleName = ["Anne", "Inger", "Kari", "Marit", "Ingrid"]

              femaleName = femaleName.filter(name => name !== 'Inger')

              console.log(femaleName);

              console.log(femaleName.includes('Inger'));






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 20:55









              WillWill

              1,82911211




              1,82911211

























                  2














                  The issue is this line:



                  if (aTable[1] === aName) {


                  That only checks the second index ("Inger"). It should be this:



                  if (aTable[i] === aName) {





                  share|improve this answer




























                    2














                    The issue is this line:



                    if (aTable[1] === aName) {


                    That only checks the second index ("Inger"). It should be this:



                    if (aTable[i] === aName) {





                    share|improve this answer


























                      2












                      2








                      2







                      The issue is this line:



                      if (aTable[1] === aName) {


                      That only checks the second index ("Inger"). It should be this:



                      if (aTable[i] === aName) {





                      share|improve this answer













                      The issue is this line:



                      if (aTable[1] === aName) {


                      That only checks the second index ("Inger"). It should be this:



                      if (aTable[i] === aName) {






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 20:50









                      Jack BashfordJack Bashford

                      17.1k51849




                      17.1k51849























                          2














                          You could do this:



                          function removeElement(aTable, aName) {
                          const index = aTable.indexOf(aName);
                          if (index > -1) {
                          aTable.splice(index, 1);
                          document.write(aTable);
                          } else {
                          document.write(aName + " is not in the list");
                          }
                          }


                          Or you could always use functional Array.prototype.filter().






                          share|improve this answer






























                            2














                            You could do this:



                            function removeElement(aTable, aName) {
                            const index = aTable.indexOf(aName);
                            if (index > -1) {
                            aTable.splice(index, 1);
                            document.write(aTable);
                            } else {
                            document.write(aName + " is not in the list");
                            }
                            }


                            Or you could always use functional Array.prototype.filter().






                            share|improve this answer




























                              2












                              2








                              2







                              You could do this:



                              function removeElement(aTable, aName) {
                              const index = aTable.indexOf(aName);
                              if (index > -1) {
                              aTable.splice(index, 1);
                              document.write(aTable);
                              } else {
                              document.write(aName + " is not in the list");
                              }
                              }


                              Or you could always use functional Array.prototype.filter().






                              share|improve this answer















                              You could do this:



                              function removeElement(aTable, aName) {
                              const index = aTable.indexOf(aName);
                              if (index > -1) {
                              aTable.splice(index, 1);
                              document.write(aTable);
                              } else {
                              document.write(aName + " is not in the list");
                              }
                              }


                              Or you could always use functional Array.prototype.filter().







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 16 '18 at 23:24









                              Christopher Bradshaw

                              95311330




                              95311330










                              answered Nov 16 '18 at 21:00









                              KhaltKhalt

                              312




                              312























                                  1














                                  Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result






                                  const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                  function notify(criteria) {
                                  const position = getPosition(criteria);
                                  const removed = removeName(position);

                                  console.log(names);
                                  console.log(`${removed} is not in the list`);
                                  }

                                  function getPosition(criteria) {
                                  return names.findIndex(name => name === criteria);
                                  }

                                  function removeName(position) {
                                  return names.splice(position, 1);
                                  }

                                  notify('Inger');








                                  share|improve this answer




























                                    1














                                    Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result






                                    const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                    function notify(criteria) {
                                    const position = getPosition(criteria);
                                    const removed = removeName(position);

                                    console.log(names);
                                    console.log(`${removed} is not in the list`);
                                    }

                                    function getPosition(criteria) {
                                    return names.findIndex(name => name === criteria);
                                    }

                                    function removeName(position) {
                                    return names.splice(position, 1);
                                    }

                                    notify('Inger');








                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result






                                      const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                      function notify(criteria) {
                                      const position = getPosition(criteria);
                                      const removed = removeName(position);

                                      console.log(names);
                                      console.log(`${removed} is not in the list`);
                                      }

                                      function getPosition(criteria) {
                                      return names.findIndex(name => name === criteria);
                                      }

                                      function removeName(position) {
                                      return names.splice(position, 1);
                                      }

                                      notify('Inger');








                                      share|improve this answer













                                      Following your description, try this example. That first gets the index in the array of the searched name using findIndex, then removes it from the array using splice and finally prints the result






                                      const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                      function notify(criteria) {
                                      const position = getPosition(criteria);
                                      const removed = removeName(position);

                                      console.log(names);
                                      console.log(`${removed} is not in the list`);
                                      }

                                      function getPosition(criteria) {
                                      return names.findIndex(name => name === criteria);
                                      }

                                      function removeName(position) {
                                      return names.splice(position, 1);
                                      }

                                      notify('Inger');








                                      const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                      function notify(criteria) {
                                      const position = getPosition(criteria);
                                      const removed = removeName(position);

                                      console.log(names);
                                      console.log(`${removed} is not in the list`);
                                      }

                                      function getPosition(criteria) {
                                      return names.findIndex(name => name === criteria);
                                      }

                                      function removeName(position) {
                                      return names.splice(position, 1);
                                      }

                                      notify('Inger');





                                      const names = ['Anne', 'Inger', 'Kari', 'Marit', 'Ingrid'];

                                      function notify(criteria) {
                                      const position = getPosition(criteria);
                                      const removed = removeName(position);

                                      console.log(names);
                                      console.log(`${removed} is not in the list`);
                                      }

                                      function getPosition(criteria) {
                                      return names.findIndex(name => name === criteria);
                                      }

                                      function removeName(position) {
                                      return names.splice(position, 1);
                                      }

                                      notify('Inger');






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 21:15









                                      user615274user615274

                                      1,65611423




                                      1,65611423






























                                          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%2f53345163%2fhow-to-use-a-for-loop-and-splice-to-remove-a-word-and-then-check-an-array-for-a%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