javascript
1 2$(function() { 3 4 var moji = new Array("a","b","c","d","e","f","g","h","i", 5 "j","k","l","m","n","o","p","q","r", 6 "s","t","u","v","w","x","y","z", ";", ",", ".", "/"); 7 8 var kcode = new Array(65,66,67,68,69,70,71,72,73, 9 74,75,76,77,78,79,80,81,82, 10 83,84,85,86,87,88,89,90,187,188,190,191); 11 12 var words = [ 13 "word","simple" 14 ]; 15 16 var key; 17 18 var currentWord=""; 19 var currentLocation; 20 21 function init() { 22 currentWord = setTarget(); 23 currentLocation = 0; 24 $('#target').text(currentWord); 25 } 26 init(); 27 28 function setTarget() { 29 currentWord = words[Math.floor(Math.random() * words.length)]; 30 $('#target').text(currentWord); 31 currentLocation = 0; 32 } 33 34 $(document).on('keydown', function(e) { 35 key = currentWord[currentLocation]; 36 if(e.keyCode === kcode[moji.indexOf(key)]) { 37 currentLocation++; 38 var placeholder = ''; 39 for(var i=0; i<currentLocation; i++) { 40 placeholder += '_'; 41 } 42 $('#target').text(placeholder + currentWord.substring(currentLocation)); 43 if(currentLocation === currentWord.length) { 44 setTarget(); 45 } 46 } 47 }); 48});
このjavascriptを入れたhtmlを実行して動かそうとしたらエラーがでます。
key = currentWord[currentLocation];の部分です。このあたりでconsole.log(currentWord);とするとundifindeになっていました。どうやったら動きますか?よろしくお願いします。
追記いたします。申し訳ございません。エラー文も書くべきでした。
Uncaught TypeError: Cannot read property '0' of undefined
at HTMLDocument.<anonymous> (javascript.js:34)
です。これから気をつけたいと思います。
回答1件
あなたの回答
tips
プレビュー