JavaScript’te Dizi Üzerinde Dön ve Eşleşmeleri Say
Senaryo
Aynı countOpenRooms testlerini JavaScript’te çalıştır.
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.
| Seçenek | Sonuç |
|---|---|
| countOpenRooms([]) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
| countOpenRooms([1]) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
| for (const seats of seatCounts) ve for (const k in seatCounts) | Sonucu görmek için yukarıdan bir seçenek tıkla. |
Açıklamayı göster
- countOpenRooms([])
0 — Python ile birebir aynı.
- countOpenRooms([1])
1 — Python ile birebir aynı.
- 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.
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
Tarayıcı ilerlemeni kaydedemedi. Değişiklikler bu sayfadan ayrılana kadar geçerli.