Return the first word with the greatest number of repeated letters











up vote
0
down vote

favorite












I'm learning javascript and have written a function that accepts a string and returns the first word with the greatest number of repeated letters. For example:



Input:"Hello apple pie" - Output:"Hello"

Input:"Hello apple pie yelllow" - Output:"yelllow"



I've tried to keep it as concise as possible. I wonder whether later on in the code it starts to become more verbose. The answer it returns seems to be correct for all tests.



Could someone give some advice about improving my code? Or is it not bad? And any tips about improving readability? Thanks for any help here.



function whichWord(str) {

// 1. split into array with words - EG if var str = "wodrd sy hello";
var arr = str.split(' '); // puts str into array ["wodrd", "sy", "hello"]
var mdArr = arr.map(function(el){ return el.split('') }); // puts str into MD array [["w","o","d","r","d"],["s","y"],["h","e","l","l","o"]]

// 2. get each unique letter (remove duplicates)
function removeDuplicates(a) {
 return Array.from(new Set(a)) 
}
var letters = mdArr.map(removeDuplicates);

// 3. count # of each letter occurrences
var temp = ;
var numbers = ;

for(i=0;i < mdArr.length;i++) {

for(k=0;k < letters[i].length;k++) {temp.push(mdArr[i].filter(function(c2){return c2==letters[i][k]}).length)}

numbers.push(temp);
temp=;

}

var letterCount = letters.map((innerArr, i) => innerArr.map((letter, ii) => ({letter: letter, instances: numbers[i][ii]}))); // puts into MD array with objects:
/* returns: [
[{letter: "w", instances: 1},{letter: "o", instances: 1},{letter: "d", instances: 2},{letter: "r", instances: 1}],
[{letter: "s", instances: 1},{letter: "y", instances: 1}],
[{letter: "h", instances: 1},{letter: "e", instances: 1},{letter: "l", instances: 2},{letter: "o", instances: 1}]
] */

// 4. return word from index with largest number, & first instance if matching numbers
var highestNumbers = letterCount.map((outerArr,i) => Math.max.apply(Math,outerArr.map((innerArr,ii) => innerArr.instances)));

var highestNumber = Math.max.apply(Math,highestNumbers);

var indexToReturn = highestNumbers.findIndex(function(c) { return c == highestNumber } );

return arr[indexToReturn];

}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I'm learning javascript and have written a function that accepts a string and returns the first word with the greatest number of repeated letters. For example:



    Input:"Hello apple pie" - Output:"Hello"

    Input:"Hello apple pie yelllow" - Output:"yelllow"



    I've tried to keep it as concise as possible. I wonder whether later on in the code it starts to become more verbose. The answer it returns seems to be correct for all tests.



    Could someone give some advice about improving my code? Or is it not bad? And any tips about improving readability? Thanks for any help here.



    function whichWord(str) {

    // 1. split into array with words - EG if var str = "wodrd sy hello";
    var arr = str.split(' '); // puts str into array ["wodrd", "sy", "hello"]
    var mdArr = arr.map(function(el){ return el.split('') }); // puts str into MD array [["w","o","d","r","d"],["s","y"],["h","e","l","l","o"]]

    // 2. get each unique letter (remove duplicates)
    function removeDuplicates(a) {
     return Array.from(new Set(a)) 
    }
    var letters = mdArr.map(removeDuplicates);

    // 3. count # of each letter occurrences
    var temp = ;
    var numbers = ;

    for(i=0;i < mdArr.length;i++) {

    for(k=0;k < letters[i].length;k++) {temp.push(mdArr[i].filter(function(c2){return c2==letters[i][k]}).length)}

    numbers.push(temp);
    temp=;

    }

    var letterCount = letters.map((innerArr, i) => innerArr.map((letter, ii) => ({letter: letter, instances: numbers[i][ii]}))); // puts into MD array with objects:
    /* returns: [
    [{letter: "w", instances: 1},{letter: "o", instances: 1},{letter: "d", instances: 2},{letter: "r", instances: 1}],
    [{letter: "s", instances: 1},{letter: "y", instances: 1}],
    [{letter: "h", instances: 1},{letter: "e", instances: 1},{letter: "l", instances: 2},{letter: "o", instances: 1}]
    ] */

    // 4. return word from index with largest number, & first instance if matching numbers
    var highestNumbers = letterCount.map((outerArr,i) => Math.max.apply(Math,outerArr.map((innerArr,ii) => innerArr.instances)));

    var highestNumber = Math.max.apply(Math,highestNumbers);

    var indexToReturn = highestNumbers.findIndex(function(c) { return c == highestNumber } );

    return arr[indexToReturn];

    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm learning javascript and have written a function that accepts a string and returns the first word with the greatest number of repeated letters. For example:



      Input:"Hello apple pie" - Output:"Hello"

      Input:"Hello apple pie yelllow" - Output:"yelllow"



      I've tried to keep it as concise as possible. I wonder whether later on in the code it starts to become more verbose. The answer it returns seems to be correct for all tests.



      Could someone give some advice about improving my code? Or is it not bad? And any tips about improving readability? Thanks for any help here.



      function whichWord(str) {

      // 1. split into array with words - EG if var str = "wodrd sy hello";
      var arr = str.split(' '); // puts str into array ["wodrd", "sy", "hello"]
      var mdArr = arr.map(function(el){ return el.split('') }); // puts str into MD array [["w","o","d","r","d"],["s","y"],["h","e","l","l","o"]]

      // 2. get each unique letter (remove duplicates)
      function removeDuplicates(a) {
       return Array.from(new Set(a)) 
      }
      var letters = mdArr.map(removeDuplicates);

      // 3. count # of each letter occurrences
      var temp = ;
      var numbers = ;

      for(i=0;i < mdArr.length;i++) {

      for(k=0;k < letters[i].length;k++) {temp.push(mdArr[i].filter(function(c2){return c2==letters[i][k]}).length)}

      numbers.push(temp);
      temp=;

      }

      var letterCount = letters.map((innerArr, i) => innerArr.map((letter, ii) => ({letter: letter, instances: numbers[i][ii]}))); // puts into MD array with objects:
      /* returns: [
      [{letter: "w", instances: 1},{letter: "o", instances: 1},{letter: "d", instances: 2},{letter: "r", instances: 1}],
      [{letter: "s", instances: 1},{letter: "y", instances: 1}],
      [{letter: "h", instances: 1},{letter: "e", instances: 1},{letter: "l", instances: 2},{letter: "o", instances: 1}]
      ] */

      // 4. return word from index with largest number, & first instance if matching numbers
      var highestNumbers = letterCount.map((outerArr,i) => Math.max.apply(Math,outerArr.map((innerArr,ii) => innerArr.instances)));

      var highestNumber = Math.max.apply(Math,highestNumbers);

      var indexToReturn = highestNumbers.findIndex(function(c) { return c == highestNumber } );

      return arr[indexToReturn];

      }









      share|improve this question













      I'm learning javascript and have written a function that accepts a string and returns the first word with the greatest number of repeated letters. For example:



      Input:"Hello apple pie" - Output:"Hello"

      Input:"Hello apple pie yelllow" - Output:"yelllow"



      I've tried to keep it as concise as possible. I wonder whether later on in the code it starts to become more verbose. The answer it returns seems to be correct for all tests.



      Could someone give some advice about improving my code? Or is it not bad? And any tips about improving readability? Thanks for any help here.



      function whichWord(str) {

      // 1. split into array with words - EG if var str = "wodrd sy hello";
      var arr = str.split(' '); // puts str into array ["wodrd", "sy", "hello"]
      var mdArr = arr.map(function(el){ return el.split('') }); // puts str into MD array [["w","o","d","r","d"],["s","y"],["h","e","l","l","o"]]

      // 2. get each unique letter (remove duplicates)
      function removeDuplicates(a) {
       return Array.from(new Set(a)) 
      }
      var letters = mdArr.map(removeDuplicates);

      // 3. count # of each letter occurrences
      var temp = ;
      var numbers = ;

      for(i=0;i < mdArr.length;i++) {

      for(k=0;k < letters[i].length;k++) {temp.push(mdArr[i].filter(function(c2){return c2==letters[i][k]}).length)}

      numbers.push(temp);
      temp=;

      }

      var letterCount = letters.map((innerArr, i) => innerArr.map((letter, ii) => ({letter: letter, instances: numbers[i][ii]}))); // puts into MD array with objects:
      /* returns: [
      [{letter: "w", instances: 1},{letter: "o", instances: 1},{letter: "d", instances: 2},{letter: "r", instances: 1}],
      [{letter: "s", instances: 1},{letter: "y", instances: 1}],
      [{letter: "h", instances: 1},{letter: "e", instances: 1},{letter: "l", instances: 2},{letter: "o", instances: 1}]
      ] */

      // 4. return word from index with largest number, & first instance if matching numbers
      var highestNumbers = letterCount.map((outerArr,i) => Math.max.apply(Math,outerArr.map((innerArr,ii) => innerArr.instances)));

      var highestNumber = Math.max.apply(Math,highestNumbers);

      var indexToReturn = highestNumbers.findIndex(function(c) { return c == highestNumber } );

      return arr[indexToReturn];

      }






      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 11 hours ago









      user8758206

      1473




      1473



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          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: "196"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2fcodereview.stackexchange.com%2fquestions%2f208014%2freturn-the-first-word-with-the-greatest-number-of-repeated-letters%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208014%2freturn-the-first-word-with-the-greatest-number-of-repeated-letters%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'