Ders programı

Ders programı

JavaScript’te Dizi Üzerinde Dön ve Eşleşmeleri Say

Senaryo

Aynı countOpenRooms testlerini JavaScript’te çalıştır.

Seçilen durumAşağıdan bir seçenek seç
Sonuç—
JavaScript
export function countOpenRooms(seatCounts) {
  let openRooms = 0;
  for (const seats of seatCounts) {
    if (seats > 0) openRooms += 1;
  }
  return openRooms;
}
// assert.equal(countOpenRooms([]), 0);
// assert.equal(countOpenRooms([1]), 1);
// assert.equal(countOpenRooms([0, 2, 0, 1]), 2);

Sıra sende

JavaScript’in her girdide Python ile aynı sonucu verdiğini doğrula.

Açıklamayı göster
  1. countOpenRooms([])

    0 — Python ile birebir aynı.

  2. countOpenRooms([1])

    1 — Python ile birebir aynı.

  3. for (const seats of seatCounts) ve for (const k in seatCounts)

    Değerler için for..of kullan; for..in "0", "1" metin dizinlerini verir.

Değişen sayaç için let, her turdaki öğe için const kullan ve dizi öğelerinde for..of tercih et.

Önce gözden geçir
Ortak test durumlarını indir

Sıra sende

JavaScript’te for..of ile countPassing(scores) yaz ve [], [60], [59, 60, 85] durumlarını doğrula.

Sık yapılan hata Sayacı const tanımlayıp artırmaya çalışmak veya for..in kullanmak.

Çalışmanı kontrol et

let passed = 0; for (const s of scores) { if (s >= 60) passed += 1; } return passed.

Kısa kontrol

JavaScript’te bir dizinin öğe değerlerini doğrudan hangi döngü okur?
Yanıtı ve gerekçesini göster

Doğru yanıt for (const seats of seatCounts)

for..of öğe değerlerini, for..in ise metin özellik adlarını döndürür.