समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो एक स्ट्रिंग str लेता है जो एक वाक्य को दर्शाता है, एकमात्र तर्क के रूप में।
हमारे फ़ंक्शन को स्ट्रिंग str में मौजूद समान आसन्न शब्द जोड़े की गिनती करके वापस करनी चाहिए। हमारे फ़ंक्शन को शब्दों की जांच करते समय उनके केस को नजरअंदाज करना चाहिए, जिसका अर्थ है 'it' और 'It' को समान गिना जाना चाहिए।
उदाहरण के लिए, यदि फ़ंक्शन का इनपुट है -
इनपुट
const str = 'This this is a a sample string';
आउटपुट
const output = 2;
आउटपुट व्याख्या
क्योंकि दोहराए जाने वाले शब्द 'this' और 'a' हैं।
उदाहरण
निम्नलिखित कोड है -
const str = 'This this is a a sample string';
const countIdentical = (str = '') => {
const arr = str.split(' ');
let count = 0;
for (let i = 0; i < arr.length - 1; i++) {
const curr = arr[i];
const next = arr[i + 1];
if (curr.toLowerCase() === next.toLowerCase()) {
count++;
}
}
return count;
};
console.log(countIdentical(str));
आउटपुट
2