0
我試圖讓用戶可以在其中放置文本的文本區域;基本上當用戶點擊按鈕時,它應該計算單詞的數量,最短單詞的長度和單詞;也就是單詞的平均長度和單詞長度的RMS(均方根)。文本區域將預加載文本,但是當用戶點擊文本區域時應該清除文本區域他們可以放任何他們想要的。這是我走到這一步:根據用戶的輸入計算字數
$(document).ready(function() {
$("#btnCalculate").click(function(){
var text = $("#txtInput").val();
text = text.replace(/\.|,|;/g, ""); //eliminate punctuation
//the g makes it a global replace not a replacement of the first occurrence
text = text.toLowerCase(); //put all text into lower case
text = text.replace(/\bi\b/g, "I");
// \b means word boundary so \bi\b means an i by iteslf which should be I
text = text.replace(/\s+/g, " "); //replace white space with a simple space
if (text.charAt(text.length - 1) == " ") {
text = text.substring(0, text.length - 1); // if space at end get rid of
}
//longest word count
function longestWord(str) {
var words = str.replace(/[^A-Za-z\s]/g, "").split(" ");
var wordsByDescendingLength = words.sort(function (a, b) {
return b.length - a.length;
});
return wordsByDescendingLength[0];
}
});
//shortest word
});
謝謝,我欣賞它 – braum 2015-02-23 04:01:25