タイピングゲームを作っています。単語を打ち終わったら次の単語を表示したいです。
コードを何度も見直していますが,どこがいけないのかわかりません。。。
サイトの正しいコードも貼り付けています。
違いが見つけられません。
宜しくお願い致します。
サイトの正しいコード
JavaScript
1'use strict'; 2 3{ 4 const words = [ 5 'apple', 6 'sky', 7 'blue', 8 'middle', 9 'set', 10 ]; 11 let word = words[Math.floor(Math.random() * words.length)]; 12 let loc = 0; 13 let score = 0; 14 let miss = 0; 15 16 const target = document.getElementById('target'); 17 const scoreLabel = document.getElementById('score'); 18 const missLabel = document.getElementById('miss'); 19 20 target.textContent = word; 21 22 function updateTarget() { 23 let placeholder = ''; 24 for (let i = 0; i < loc; i++) { 25 placeholder += '_'; 26 } 27 target.textContent = placeholder + word.substring(loc); 28 } 29 30 window.addEventListener('keydown', e => { 31 if (e.key === word[loc]) { 32 loc++; 33 if (loc === word.length) { 34 word = words[Math.floor(Math.random() * words.length)]; 35 loc = 0; 36 } 37 updateTarget(); 38 score++; 39 scoreLabel.textContent = score; 40 } else { 41 miss++; 42 missLabel.textContent = miss; 43 } 44 }); 45}
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8"> 5 <title>Typing Game</title> 6 <link rel="stylesheet" href="css/styles.css"> 7</head> 8<body> 9 <p id="target">word</p> 10 <p class="info"> 11 Letter count: <span id="score">0</span>, 12 Miss count: <span id="miss">0</span> 13 </p> 14 15 <script src="js/main.js"></script> 16</body> 17</html>
自分のコード
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>タイピングゲーム</title> 7 <link rel="stylesheet" href="css/styles.css"> 8</head> 9<body> 10 <p id="target">word</p> 11 <p class="info"> 12 Letter score<span id="score">0</span> 13 Miss score<span id="miss">0</span> 14 </p> 15 <script src="js/main.js"></script> 16</body> 17</html>
JavaScript
1"use strict"; 2 3{ 4 const words=[ 5 "apple", 6 "lemon", 7 "banana", 8 "melon", 9 ]; 10 let word =words[Math.floor(Math.random()*words.length)]; 11 let score=0; 12 let miss=0; 13 let loc = 0; 14 15 const target = document.getElementById("target"); 16 const scoreLabel=document.getElementById("score"); 17 const missLabel=document.getElementById("miss"); 18 19 target.textContent=word; 20 21 function updateTarget(){ 22 let placeholder =""; 23 for(let i =0; i<loc ; i++){ 24 placeholder += "_"; 25 } 26 target.textContent=placeholder+word.substring(loc); 27 } 28 29 window.addEventListener("keydown",e=>{ 30 if(e.key===word[loc]){ 31 loc++; 32 if(loc===word.lenght){ 33 word=words[Math.floor(Math.random()*words.length)]; 34 loc=0; 35 } 36 updateTarget(); 37 score++; 38 scoreLabel.textContent=score; 39 }else{ 40 missLabel.textContent=miss; 41 miss++; 42 } 43 }); 44} 45
css
1body { 2 padding-top: 40px; 3 font-family: 'Courier New', monospace; 4 text-align: center; 5} 6 7#target { 8 font-size: 48px; 9 letter-spacing: 3px; 10} 11 12.info { 13 color: #ccc; 14}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/03/04 11:58
退会済みユーザー
2020/03/04 12:00