Series: JavaScript Masalalari 7

So‘zlar soni

Berilgan gapda nechta so‘z borligini aniqlaydigan funksiya yozing.

Masala

Berilgan gapda nechta so‘z borligini toping.

  • Funksiya nomi: countWords
  • Parametr: matn sentence (bir nechta bo‘sh joy bilan ajratilgan so‘zlar)
  • Natija: butun son — so‘zlar soni (ketma-ket bo‘sh joylarni bitta ajratuvchi deb hisoblang)

Misollar

countWords("salom dunyo js"); // 3
countWords("bir ikki"); // 2
countWords(""); // 0
countWords("   "); // 0

console.log(...) orqali tekshiring.

Practice & Try It Out

Note: Use console.log() to print output

Write your code here and click "Run" to see the output

Output