2017-08-01 7 views
0

私はjavascriptのコーディングに初心者ですし、私はそれがjavascriptのエラー:キャッチされないでSyntaxError:予期しない識別子

コードが何であるかを教えてChromeブラウザに私の最初のプログラムをロードしたとき、私はこのエラーを得ています:

var target; 
var select; 
var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"]; 
var finished = false; 

function do_game() { 
    var random_color = Math.floor(Math.random() * colors.length); 
    target = colors[random_color]; 
} 
while (!finished) { 
    select = prompt("I am thinking of one of these colors\n\n brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i thinking of"); 
    finished = check_guess(); 
} 

function check_guess() { 
    if (select == target) { 
    return true; 
    } else { 
    return false; 
    } 

} 
+3

あなたのwhileループは、関数の外に浮遊している....あなたはdo_gameでループにしたいですか? –

+1

そのように、問題はないようです(クロムコンソールにコピーして貼り付けているようですが)@Rick Brongerはおそらくあなたのwhileループは 'do_game'関数の中になければならないと言っています – Xatyrian

+0

はい、do_gameにループを挿入したのですが同じエラーを表示しています –

答えて

0
<script> 

var target; 
var select; 
var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"]; 
var finished = false; 

function do_game() { 
    var random_color = Math.floor(Math.random() * colors.length); 
    target = colors[random_color]; 
    while (!finished) { 
     select = prompt("I am thinking of one of these colors\n\n brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i thinking of"); 
     finished = check_guess(); 
    } 
} 
function check_guess() { 
    if (select == target) { 
     return true; 
    } else { 
     return false; 
    } 
} 

do_game(); 

</script> 

私はすべての生ステートメントが非常に単純であっても、関数に囲まれなければならないと思います。希望を助けてください。

0

あなたの問題はあなたがdo_game関数を呼び出すことはありませんということですので、変数が初期化されることはありませんし、ループを終了することはありませんターゲット、 は、このチェック:あなたが言及し、実際にエラーがないことを

var target; 
var select; 
var colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", 
"white", "purple", "pink"]; 
var finished = false; 

do_game(); 
startGame(); 


function do_game() { 
var random_color = Math.floor(Math.random() * colors.length); 
target = colors[random_color]; 
console.log(target) 
} 

function startGame() { 
while (!finished) { 
select = prompt("I am thinking of one of these colors\n\n 
brown,cyan,yellow,red,blue,green,black,white,purple,pink\n what color am i 
thinking of"); 
finished = check_guess(); 
} 
} 
    function check_guess() { 
    if (select == target) { 
    return true; 
    } else { 
    return false; 
    } 
    } 

https://jsfiddle.net/4qops0br/1/

3

をあなたが提供したコードで発生します。私はあなたがonclick属性のようなHTML属性のdo_gameへの呼び出しを持っていて、間違ったカンマや予約語の使用、または他の多くの構文問題のような小さなタイプミスがあると思います。

あなたべきである:

  • コールdo_game
  • がその関数

にループを入れて、さらに向上させる(それはあなたが提供するコードに呼び出されることはありません):

  • ユーザーがプロンプトをキャンセルしたときに検出します。
  • グローバル変数の代わりにローカル変数を使用し、必要なときに関数パラメータを介して変数を渡します。真の定数はグローバルにすることができます。
  • if (comparison) return true else return falseパターンを避ける代わりにvar
  • letconstを利用します。ちょうどreturn comparisonを実行してください。
  • あなたが正しく推測したときにユーザに知らせる
  • すでに色の配列があるので、質問のリテラル文字列としてもう一度繰り返しませんが、その配列を再利用してください。

// The list of colors can be global, but the rest should be locally defined 
 
const colors = ["brown", "cyan", "yellow", "red", "blue", "green", "black", "white", "purple", "pink"]; 
 

 
function do_game() { 
 
    const random_color = Math.floor(Math.random() * colors.length); 
 
    // Make your variables local to the function 
 
    const target = colors[random_color]; 
 
    let finished = false; 
 
    
 
    while (!finished) { 
 
     // Actually declare `select`, or it will be global. 
 
     // Reuse the variable with colors to build the question 
 
     // (the commas will be inserted by the automatic conversion to string) 
 
     const select = prompt("I am thinking of one of these colors\n\n" 
 
      + colors + ".\n\n What color am I thinking of?"); 
 
     // Exit game when prompt was cancelled 
 
     if (select === null) return; 
 
     // Pass the necessary info to the other function (instead of using globals): 
 
     finished = check_guess(select, target); 
 
    } 
 
    // Let the user know that they guessed it 
 
    alert('You guessed it: ' + target); 
 
} 
 

 
function check_guess(select, target) { 
 
    // No need to if-else a true/false, just return the comparison's result 
 
    return select == target; 
 
} 
 

 
// Start the game 
 
do_game();

+0

良い答え、imho、const初期化とゲームループは、より洗練されたコードのために、別の関数にすべきですが、素敵な答え – Ferus7

関連する問題