2016-11-09 4 views
0

Ich versuche, den Buchstaben zu finden, dass die meisten Wörter in einem Satz enden mit und alle Wörter, die mit diesem Buchstaben enden.Funktion, um den häufigsten Buchstaben zu finden, mit dem Wörter in einem Satz enden

Es ist meine Hausaufgaben und ich habe die Split-Funktion versucht, aber es hilft nicht und die Slice-Funktion, aber es dauert zu lange.

var sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.' 

function mostWordsEndsWith(){ 
    for (var i = 0; i < wordsEndsWith.length; i++) { 
     var currentWord = wordsEndWith[i].split('t') 
     console.log(currentWord); 
    } 
} 
mostWordsEndsWith(sentence); 
+0

Warum hilft 'Split' nicht? – Filburt

+1

Ich rate, dass Sie einfach eine Reihe von Wörtern durchlaufen können, '' yourArray [Index] .substring (yourArray [index] .length - 1, yourArray [index] .length); '' und zu einem Zähler hinzufügen wenn du gehst. Langsam, aber einfach. Ein noch besserer Weg wäre, die Teilstrings in einem separaten Array zu sammeln und dann eine weitere Schleife zu erstellen, um diese separaten Arrays zu durchlaufen, um die längste zu finden. – Crowes

+0

Ich würde vorschlagen, dass Sie versuchen, bis Sie zumindest mit etwas Code kommen können, der tatsächlich etwas tut - derzeit akzeptiert Ihre Funktion 'mostWordsEndsWith' nicht einmal den Parameter, mit dem Sie versuchen, es zu nennen, und Ihr Array' wordsEndsWith' isn ' t irgendwo definiert. – Filburt

Antwort

-1

Hier ist eine Möglichkeit, das zu tun. Beachten Sie, dass es mehrere ES6-Funktionen verwendet, falls die Kompatibilität ein Problem darstellt.

var sentence = "Down by the river there is a man that quiver" 
 
      + " and shiver, but he needs to deliver a packet" 
 
      + " that he think is a big racket and a packet" 
 
      + " of of gum." 
 

 
function mostWordsEndsWith (str){ 
 
    //index for: letter --> { count, words } 
 
    //this is quite a good object itself, however not yet the 
 
    //desired final result 
 
    let endLetterCount = 
 
     //get array of words 
 
     str.match(/\b\w+\b/g) 
 
      //add information about last letter. 
 
      //There is no error handling, but looking what was 
 
      //matched before, something must go very wrong 
 
      //for it to not match. 
 
      .map((w) => ({ 
 
       word: w, 
 
       last: w.match(/(\w)(?:[^\w]|$)/)[1] 
 
      })) 
 
      //create index 
 
      .reduce((p, c) => { 
 
       if (p.hasOwnProperty(c.last)) { 
 
       p[c.last].count++; 
 
       p[c.last].words.push(c.word); 
 
       } else p[c.last] = { count: 1, words: [c.word] }; 
 
       return p; 
 
      }, 
 
      //ugly, hi javascript... 
 
      //There are lengthy discussions on why objects 
 
      //dont per default work similar to this, not too 
 
      //relevant for this question. 
 
      { 
 
       [Symbol.iterator]: function*(){ 
 
       //Here we know exactly what the object is, 
 
       //so issues regarding "own property" are 
 
       //non-existant 
 
       for (prop in this) 
 
        yield { key: prop, value: this[prop] }; 
 
       } 
 
      }); 
 
    
 
    //get the letter with the highest count 
 
    let highestCount = [...endLetterCount].reduce(
 
    (p, c) => c.value.count > p.value.count ? c : p 
 
); 
 
    
 
    //return some nice output format, whatever is wanted. 
 
    return `Letter "${highestCount.key}" occurred most, ` 
 
     + `${highestCount.value.count} times: ` 
 
     + `${highestCount.value.words}.`; 
 
} 
 

 
console.log(mostWordsEndsWith(sentence));

0

let sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.'; 
 

 
let words = sentence.match(/(\w+)/g); 
 

 
let letters = {}; 
 

 
//Create an array of object containing the word and his last latter 
 
words = words.map(function(word) { 
 
    return { 
 
    word: word, 
 
    lastLetter: word.slice(-1) 
 
    } 
 
}); 
 

 
//Add the number of occurence for each letters inside the letters object 
 
words.forEach(function(elem) { 
 
    letters[elem.lastLetter] = letters[elem.lastLetter] || 0; 
 
    letters[elem.lastLetter] ++; 
 
}); 
 

 
//Sort the letters object and take the first one 
 
let mostPresentLetter = Object.keys(letters).sort(function(a, b) { 
 
    return letters[b] - letters[a] 
 
})[0]; 
 

 
//Display the most present letter 
 
console.log("Most present letter : " + mostPresentLetter); 
 
//Display all the letters. map is used to display the word only 
 
console.log("Words : " + words.filter(function(item) { 
 
    return item.lastLetter === mostPresentLetter; 
 
}).map(function(o) { 
 
    return o.word; 
 
}));

+0

@Downvoter eine Erklärung bitte? Warum benötigt es einen Downvote? – Weedoze

+0

Überprüfung Ihres Codes scheint es, dass 'word.slice (-1)' würde einige Interpunktion wie Kommas usw. aufgreifen, da die 'split' durch den Raum nicht garantiert, mit einem tatsächlichen Buchstaben zu enden, also ist es kein guter Algorithmus zur Worterkennung – arhak

+0

@arhak Antwort aktualisiert. Es ist jetzt ein Regex – Weedoze

0

hier haben Sie eine ziemlich einfache Methode, die mit alten JS

var sentence = 'Down by the river there is a man that quiver and shiver, but he needs to deliver a packet that he think is a big racket and a packet of of gum.' 
 

 
var census = {}; 
 
var most; 
 

 
sentence.match(/(\w+)\b/g).forEach(function(word) { 
 
    var last = word.substr(-1); 
 
    most = most || last; 
 
    census[last] = census[last] || []; 
 
    // a word that appears more than once is counted each time 
 
    census[last].push(word); 
 
    if (census[last].length > census[most].length) { 
 
    most = last; 
 
    } 
 
}); 
 

 
//console.log(census); 
 
console.log('most repeated letter was: ' + most + ' (' + census[most].length + ' times)'); 
 
console.log('and corresponding words were: ' + census[most]);
kompatibel ist

Verwandte Themen