Ternary operator function for JavaScript 'get middle letter' Code War challenge












0















Code challenge: write a function that gets the middle letter(s) of a string of even or odd numbered letters, as a ternary operator.



My function is working for odd numbers. But it does not work for even numbers - it seems to be skipping letters.



eg.



getMiddle("testing") // the output is "t", this is correct.  
getMiddle("test") // the output is "et", it should be "es".


my code:



    function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length/2 - 1] + str[str.length/2 + 1] : str[Math.floor(str.length/2)];
}









share|improve this question


















  • 1





    When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

    – Pointy
    Nov 23 '18 at 15:00






  • 1





    The answer is very easy, I think you could see it if you try more

    – Cristian Traìna
    Nov 23 '18 at 15:02
















0















Code challenge: write a function that gets the middle letter(s) of a string of even or odd numbered letters, as a ternary operator.



My function is working for odd numbers. But it does not work for even numbers - it seems to be skipping letters.



eg.



getMiddle("testing") // the output is "t", this is correct.  
getMiddle("test") // the output is "et", it should be "es".


my code:



    function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length/2 - 1] + str[str.length/2 + 1] : str[Math.floor(str.length/2)];
}









share|improve this question


















  • 1





    When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

    – Pointy
    Nov 23 '18 at 15:00






  • 1





    The answer is very easy, I think you could see it if you try more

    – Cristian Traìna
    Nov 23 '18 at 15:02














0












0








0








Code challenge: write a function that gets the middle letter(s) of a string of even or odd numbered letters, as a ternary operator.



My function is working for odd numbers. But it does not work for even numbers - it seems to be skipping letters.



eg.



getMiddle("testing") // the output is "t", this is correct.  
getMiddle("test") // the output is "et", it should be "es".


my code:



    function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length/2 - 1] + str[str.length/2 + 1] : str[Math.floor(str.length/2)];
}









share|improve this question














Code challenge: write a function that gets the middle letter(s) of a string of even or odd numbered letters, as a ternary operator.



My function is working for odd numbers. But it does not work for even numbers - it seems to be skipping letters.



eg.



getMiddle("testing") // the output is "t", this is correct.  
getMiddle("test") // the output is "et", it should be "es".


my code:



    function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length/2 - 1] + str[str.length/2 + 1] : str[Math.floor(str.length/2)];
}






javascript algorithm ternary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:58









Hm 13Hm 13

31




31








  • 1





    When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

    – Pointy
    Nov 23 '18 at 15:00






  • 1





    The answer is very easy, I think you could see it if you try more

    – Cristian Traìna
    Nov 23 '18 at 15:02














  • 1





    When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

    – Pointy
    Nov 23 '18 at 15:00






  • 1





    The answer is very easy, I think you could see it if you try more

    – Cristian Traìna
    Nov 23 '18 at 15:02








1




1





When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

– Pointy
Nov 23 '18 at 15:00





When it's even, you get the letters at positions one before and one after half the length; you skip the letter at exactly half the length.

– Pointy
Nov 23 '18 at 15:00




1




1





The answer is very easy, I think you could see it if you try more

– Cristian Traìna
Nov 23 '18 at 15:02





The answer is very easy, I think you could see it if you try more

– Cristian Traìna
Nov 23 '18 at 15:02












2 Answers
2






active

oldest

votes


















2














When the length is is even, you want the 2 characters which are at the middle. You do that by taking the length of the string and divide it by 2.



That index in a zero based array will be the s.If you subtract 1 from the index, it will be the e. When you add +1, you get the t



In your code, you concatenate the index -1 and the index +1 leading to et



You should omit the + 1 in str[str.length/2 + 1] like:



function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
}





function getMiddle(str) {
return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
}

console.log(getMiddle("testing"));
console.log(getMiddle("test"));
console.log(getMiddle("testtest"))








share|improve this answer

































    0














    I understand a use of ternary is required but you may simply do this without any such conditionals as well.






    var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
    console.log(getmid("test")); // <- "es"
    console.log(getmid("testo")); // <- "s"








    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%2f53448925%2fternary-operator-function-for-javascript-get-middle-letter-code-war-challenge%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









      2














      When the length is is even, you want the 2 characters which are at the middle. You do that by taking the length of the string and divide it by 2.



      That index in a zero based array will be the s.If you subtract 1 from the index, it will be the e. When you add +1, you get the t



      In your code, you concatenate the index -1 and the index +1 leading to et



      You should omit the + 1 in str[str.length/2 + 1] like:



      function getMiddle(str) {
      return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
      }





      function getMiddle(str) {
      return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
      }

      console.log(getMiddle("testing"));
      console.log(getMiddle("test"));
      console.log(getMiddle("testtest"))








      share|improve this answer






























        2














        When the length is is even, you want the 2 characters which are at the middle. You do that by taking the length of the string and divide it by 2.



        That index in a zero based array will be the s.If you subtract 1 from the index, it will be the e. When you add +1, you get the t



        In your code, you concatenate the index -1 and the index +1 leading to et



        You should omit the + 1 in str[str.length/2 + 1] like:



        function getMiddle(str) {
        return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
        }





        function getMiddle(str) {
        return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
        }

        console.log(getMiddle("testing"));
        console.log(getMiddle("test"));
        console.log(getMiddle("testtest"))








        share|improve this answer




























          2












          2








          2







          When the length is is even, you want the 2 characters which are at the middle. You do that by taking the length of the string and divide it by 2.



          That index in a zero based array will be the s.If you subtract 1 from the index, it will be the e. When you add +1, you get the t



          In your code, you concatenate the index -1 and the index +1 leading to et



          You should omit the + 1 in str[str.length/2 + 1] like:



          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }





          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }

          console.log(getMiddle("testing"));
          console.log(getMiddle("test"));
          console.log(getMiddle("testtest"))








          share|improve this answer















          When the length is is even, you want the 2 characters which are at the middle. You do that by taking the length of the string and divide it by 2.



          That index in a zero based array will be the s.If you subtract 1 from the index, it will be the e. When you add +1, you get the t



          In your code, you concatenate the index -1 and the index +1 leading to et



          You should omit the + 1 in str[str.length/2 + 1] like:



          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }





          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }

          console.log(getMiddle("testing"));
          console.log(getMiddle("test"));
          console.log(getMiddle("testtest"))








          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }

          console.log(getMiddle("testing"));
          console.log(getMiddle("test"));
          console.log(getMiddle("testtest"))





          function getMiddle(str) {
          return str.length % 2 === 0 ? str[str.length / 2 - 1] + str[str.length / 2] : str[Math.floor(str.length / 2)];
          }

          console.log(getMiddle("testing"));
          console.log(getMiddle("test"));
          console.log(getMiddle("testtest"))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 15:19

























          answered Nov 23 '18 at 15:02









          The fourth birdThe fourth bird

          21.3k81326




          21.3k81326

























              0














              I understand a use of ternary is required but you may simply do this without any such conditionals as well.






              var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
              console.log(getmid("test")); // <- "es"
              console.log(getmid("testo")); // <- "s"








              share|improve this answer




























                0














                I understand a use of ternary is required but you may simply do this without any such conditionals as well.






                var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
                console.log(getmid("test")); // <- "es"
                console.log(getmid("testo")); // <- "s"








                share|improve this answer


























                  0












                  0








                  0







                  I understand a use of ternary is required but you may simply do this without any such conditionals as well.






                  var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
                  console.log(getmid("test")); // <- "es"
                  console.log(getmid("testo")); // <- "s"








                  share|improve this answer













                  I understand a use of ternary is required but you may simply do this without any such conditionals as well.






                  var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
                  console.log(getmid("test")); // <- "es"
                  console.log(getmid("testo")); // <- "s"








                  var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
                  console.log(getmid("test")); // <- "es"
                  console.log(getmid("testo")); // <- "s"





                  var getmid = (s, i=s.length/2) => s.slice(Math.ceil(i-1), Math.ceil(i+1)-(s.length & 1));
                  console.log(getmid("test")); // <- "es"
                  console.log(getmid("testo")); // <- "s"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 24 '18 at 16:01









                  ReduRedu

                  12.7k22435




                  12.7k22435






























                      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%2f53448925%2fternary-operator-function-for-javascript-get-middle-letter-code-war-challenge%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

                      Berounka

                      Sphinx de Gizeh

                      Different font size/position of beamer's navigation symbols template's content depending on regular/plain...