所以我希望我的 for 回圈通過文本運行,并計算提到了多少次:“really”、“basically”和“very”。
這是正文:
let story = '上周末,我騎了一次非常漂亮的自行車。這條路線被稱為“The 9W to Nyack”,它實際上從曼哈頓的河濱公園一直延伸到新澤西州的南尼亞克。從頭到尾真的是一次冒險!這是一個 48 英里的環路,基本上花了我一整天的時間。我在河岸州立公園停下來拍了一些非常有藝術氣息的照片。不過,這是一個非常短暫的停留,因為我還有很長的路要走。;
let storyWords= story.split(' ');
這是功能:
function count() {
let reallycount = 0;
let verycount =0;
let basicallycount = 0;
for (let i = 0; i < storyWords.length; i ) {
if (storyWords[i] === 'really'){
reallycount = 1;
console.log ('really count :' reallycount);
} else if ( storyWords[i] === 'very'){
verycount = 1;
console.log ('very count :' verycount);
} else if ( storyWords[i] === 'basically'){
basicallycount = 1;
console.log ('basically count :' basicallycount);
}
}
} count();
結果:
非常數:1, 真數:1, 基本數:1, 非常數:2, 真數:2, 真數:3,
我的問題:所以我只想顯示非常、基本和真的計數的最后結果。換句話說,我希望結果是:
基本上數:1, 非常數:2, 真正數:3,
如何隱藏/洗掉其他結果?
uj5u.com熱心網友回復:
您可以將其提取到變數中,然后在控制臺記錄它
function count() {
let reallycount = 0;
let verycount = 0;
let basicallycount = 0;
for (let i = 0; i < storyWords.length; i ) {
if (storyWords[i] === "really") {
reallycount = 1;
} else if (storyWords[i] === "very") {
verycount = 1;
} else if (storyWords[i] === "basically") {
basicallycount = 1;
}
}
console.log("basically count :" basicallycount);
console.log("very count :" verycount);
console.log("really count :" reallycount);
}
count();
uj5u.com熱心網友回復:
這是您的counts()函式的一種更通用的形式,它以函式方式作業得更多:
const story = `Last weekend, I took a very beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It's really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some very artsy photos. It was a really short stop, though, because I had a really long way left to go.` ;
function counts(str, words){
const arr=str.toLowerCase().split(/\s /);
return arr.reduce((a,w)=>{
if(words.indexOf(w)>-1) a[w]=(a[w]||0) 1
return a
}, {} )
}
console.log(counts(story,["really","actually","very"]));
uj5u.com熱心網友回復:
計算單詞出現次數的另一種選擇是使用filter函式
const story = "Last weekend, I took a very beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It's really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some very artsy photos. It was a really short stop, though, because I had a really long way left to go."
const storyWords = story.split(" ")
function count() {
const basicallycount = storyWords.filter(word => word === "basically").length
const verycount = storyWords.filter(word => word === "very").length
const reallycount = storyWords.filter(word => word === "really").length
console.log(`basically count: ${basicallycount}`
console.log(`very count: ${verycount}`
console.log(`really count: ${reallycount}`
}
count();
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/392112.html
標籤:javascript 功能 循环
上一篇:公司資料格式化
下一篇:有效地回圈遍歷地圖串列
