2021年9月17日解決済み

なぜ red 以外の他の文字を入れても表示されないのですか?

ユーザー
    const word = 'red';
    let loc = 0;//位置

    const target = document.getElementById('target');
    target.textContent = word;

    document.addEventListener('keydown',e => {
        if(e.key === word[loc]){
            loc++; 

            target.textContent = '_'.repeat(loc) + word.substring(loc);
        }
    })

なぜ red 以外の他の文字を入れても表示されないのですか?
また、このソースコードのどのあたりで表題の件を表しているのでしょうか?

word で red と定義して target.textContent = word; としただけですと、 red 以外のタイピングを許さないように見えない感じがするのですが...。

スタッフ

この回答を見るにはプレミアムプランへの登録が必要です

プレミアムプランとは?