2016-10-17 3 views
2

JSFiddleのバージョンは、私が代わりに Working Codepenは、jQueryのまたはJavaScript

をcodepen追加しましたので、働いていなかったとボタンを使用して変数を更新するにはどうすればそう、私は謝罪するJavaScriptとjQueryにかなり新しいです私の言葉が正しくない場合、または答えが非常に明白な場合、私はここに学びます:)私はロックペーパーのハサミゲームを構築しました。私は変数 'c​​omputerChoice'を更新する方法を理解できません。私はページをリフレッシュするように設定することができましたが、それは私が達成したいことではありません。私はあなたが '新しいゲーム'をクリックすると、変数 'c​​omputerChoice'は、ページを更新するときのように、乱数に応じて新しいオプションを選択するようにします。私はあなたが '新しいゲーム'をクリックするとnullに設定しようとしましたが、ゲームをプレイするときに数字を返します。

HTML:

<h1>Rock Paper Scissors</h1> 
<h2>Pick your weapon!</h2> 
<p><button id="refresh">New Game</button></p> 
<button onClick='choose("rock")' class="choices">Rock</button> 
<button onClick='choose("paper")' class="choices">Paper</button> 
<button onClick='choose("scissors")' class="choices">Scissors</button> 
<p><button onClick='compare(user, computerChoice)' class="compares">1...2...3...</button></p> 
<p><b id="you">...</b> Vs. 
<b id="computer">...</b></p> 
<p><b id="results"></b></p> 

はJavaScript:

//user choices 
var user; 
var choose = function(choice) { 
user = choice; 
//document.getElementById("you").innerHTML = user; 
} 

//computer choices 
var computerChoice = Math.random(); 
if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} 
else if (computerChoice < 0.67) { 
    computerChoice = "paper"; 
} 
else { 
    computerChoice = "scissors"; 
} 

//compare user choice to computer choice 
function compare(choice1, choice2) { 
if (choice1 === choice2) { 
    document.getElementById('results').innerHTML = "How boring, you tied."; 
} 
else if (choice1 === "rock") { 
    if (choice2 === "scissors") { 
    document.getElementById('results').innerHTML = "They'll feel that in the morning."; 
} else { 
    document.getElementById('results').innerHTML = "Can you breathe? I think not."; 
} 
} 
else if (choice1 === "scissors") { 
if (choice2 === "paper") { 
    document.getElementById('results').innerHTML = "Snippitysnip, you sliced them in two."; 
} else { 
    document.getElementById('results').innerHTML = "Ouch, looking a little blunt."; 
} 
} 
else if (choice1 === "paper") { 
if (choice2 === "rock") { 
    document.getElementById('results').innerHTML = "You smothered them, eesh!" 
} else { 
    document.getElementById('results').innerHTML = "You're looking a bit like a banana split." 
} 
} 
else { 
document.getElementById('results').innerHTML = "Something's not quite right...what did you do?!" 
} 
} 

// refresh, clear and display computer choices and results. Disable, enable buttons. 
function disableButtons() { 
    $('button.choices').attr('disabled', true); 
} 

function enableButtons() { 
    $('button.choices').attr('disabled', false); 
} 

$('.choices').click(function() { 
    $('#you').html(user); // this works on codepen but not on jsfiddle? D: 
    $('#computer').html('...'); 
    disableButtons(); 
}); 
$('#refresh').click(function() { 
    $('#results').html(''); 
    $('#you, #computer').html('...'); 
    enableButtons(); 
    refreshComputer(); 
}); 
$('.compares').click(function() { 
    $('#computer').html(computerChoice); 
}); 

JSFiddleのバージョンは、私が代わりにcodepen Working Codepen

+0

「refreshComputer」はどこに定義されていますか? –

+2

コンソールを確認してください。その場にはエラーがあり、大部分またはすべてが変数または関数スコープと関係しています。 –

+0

このようなより複雑な質問については、それを[JSFiddle](https://jsfiddle.net/)または[CodePen](https://codepen.io/)にスローするのが最善でしょう。そこに役立つ解決策を見つけ出すのがずっと簡単になります。 – samanime

答えて

1

あなたのコードよさそうだが、あなたが混合バニラjQueryを使ってJavaScriptを試みたが、おそらくあなたのさまざまなオプションをテストの一環として、あなたのhtml内の一部(不要)ハンドラを追加しましたようです。

コードを少しリファクタリングして、 '1..2..3'ボタンをクリックするたびにコンピュータの選択肢をリフレッシュするようにしました。そして、あなたがいるように見えるすべての機能にJQueryを使用しますとにかくそれを使っています(JQueryを使ってhtmlを設定するか、自分の好みのinnerHTMLオプションのように、すべてのコードで同様の方法を使用するコードベースが1つあることはうれしい)。

Btw、codepenまたはjsfiddle isいいですが、このウェブサイトには完全に有効なスニペットエディタがあり、後でどのように見えるかを示すことができます)

$(function() { 
 
    // click function for choices 
 
    $('.choices').on('click', function(e) { 
 
    var choice = $(e.target).text(); 
 
    $('#you').html(choice); 
 
    disableButtons(); 
 
    }); 
 
    
 
    // click function for refresh 
 
    $('#refresh').click(function() { 
 
    $('#results').html(''); 
 
    $('#you, #computer').html('...'); 
 
    enableButtons(); 
 
    }); 
 
    
 
    // click function for 1..2..3 
 
    $('#compares').click(function() { 
 
    var computerChoice = getComputerChoice(), user = getUserChoice(); 
 
    $('#computer').html(computerChoice); 
 
    compare(user.toLowerCase(), computerChoice.toLowerCase()); 
 
    }); 
 
    
 
    // gets the previously stored userChoice 
 
    function getUserChoice() { 
 
    return $('#you').text(); 
 
    } 
 

 
    // gets a generated computer choice 
 
    function getComputerChoice() { 
 
    var computerChoice = Math.random(); 
 
    if (computerChoice < 0.34) { 
 
     computerChoice = "rock"; 
 
    } else if (computerChoice < 0.67) { 
 
     computerChoice = "paper"; 
 
    } else { 
 
     computerChoice = "scissors"; 
 
    } 
 
    return computerChoice; 
 
    } 
 

 
    //compare user choice to computer choice 
 
    function compare(choice1, choice2) { 
 
    var result; 
 
    if (choice1 === choice2) { 
 
     result = "How boring, you tied."; 
 
    } else if (choice1 === "rock") { 
 
     if (choice2 === "scissors") { 
 
     result = "They'll feel that in the morning."; 
 
     } else { 
 
     result = "Can you breathe? I think not."; 
 
     } 
 
    } else if (choice1 === "scissors") { 
 
     if (choice2 === "paper") { 
 
     result = "Snippitysnip, you sliced them in two."; 
 
     } else { 
 
     result = "Ouch, looking a little blunt."; 
 
     } 
 
    } else if (choice1 === "paper") { 
 
     if (choice2 === "rock") { 
 
     result = "You smothered them, eesh!" 
 
     } else { 
 
     result = "You're looking a bit like a banana split." 
 
     } 
 
    } else { 
 
     result = "Something's not quite right...what did you do?!" 
 
    } 
 
    $('#results').html(result); 
 
    } 
 

 
    // refresh, clear and display computer choices and results. Disable, enable buttons. 
 
    function disableButtons() { 
 
    $('button.choices').attr('disabled', true); 
 
    } 
 

 
    function enableButtons() { 
 
    $('button.choices').attr('disabled', false); 
 
    } 
 
});
body { 
 
    text-align: center; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<h1>Rock Paper Scissors</h1> 
 
<h2>Pick your weapon!</h2> 
 
<p> 
 
    <button id="refresh">New Game</button> 
 
</p> 
 
<button class="choices">Rock</button> 
 
<button class="choices">Paper</button> 
 
<button class="choices">Scissors</button> 
 
<p> 
 
    <button id="compares">1...2...3...</button> 
 
</p> 
 
<p><b id="you">...</b> Vs. 
 
    <b id="computer">...</b> 
 
</p> 
 
<p><b id="results"></b> 
 
</p>

+0

ありがとうございました!私は後でこのことを理解するために座っておく必要があります:D – lmutton

+0

私はこの行を説明することができますか?var computerChoice = getComputerChoice()、user = getUserChoice(); – lmutton

+0

@lmuttonもちろん、それぞれのメソッドから値を取得する2つの変数が宣言されています。 'getComputerChoice()'は新しく生成された値を取得し、 'getUserChoice()'はHTMLからボタンをクリックすることによって以前に設定されたテキストを取得します。これらの2つの値はパラメータとして 'compare'関数に送られます。その声明のもう一つの部分はあなたが説明したがっていますか? – Icepickle

0

あなたが作成していないのはなぜを追加しましたので、働いていませんでした関数newgameの値を与えるとそこにcomputerChoice。 は機能

var computerChoice = 0 

function newgame(){ 
computerChoice = Math.Random() 
} 

前に変数を宣言しNewGame上のボタンをクリックしたときに、この関数を呼び出します。

<button onclick="newgame()">New Game</button> 

あなたが何かをしたい場合は、私に教えてください、私は私の答えすべての

+0

これはうまくいかないように見えます。ロックペーパーやはさみの代わりに0を返します。 – lmutton

+0

@lmutton私は自分の答えを少し更新しました。また、コードをデバッグして、更新されたコードで実行した後にどこが正確に0になっているかを確認できますか? –

0

ファーストを更新します、あなたの質問に答えるために:

あなたは「リフレッシュ」することができます(値を変更)の(つまり、実行されるたびに新しい乱数を生成して返す関数を作成します)。以下の私のコードとcomputerChoice関数を見てください。

は、より詳細に続けて:あなたは間違って(少なくとも)二つのことをやっている:

  1. は、ネイティブ(インライン)onClick event handlerjQuery's click methodを混合します。プロジェクトに大きなライブラリを追加し、その手法やツールを使用していない、要素一般に

のjQueryの選択でgetElementByIdを混合

  • 使用してインラインクリックハンドラ
  • は意味がありません。

    プロジェクトでjQueryが必要な場合(おそらく単純なプログラムではないかもしれませんが)the .on() methodとすれば、より柔軟にevent delegationを実行できます。

    あなたはそれは確実だが、あなたのインラインクリックハンドラを削除する必要があります。

    <button class="js-choices">Rock</button> 
    <button class="js-choices">Paper</button> 
    <button class="js-choices">Scissors</button> 
    

    、イベントの委任を使用して、js-choices要素にイベントハンドラをアタッチ:

    $('body').on('click', '.js-choices', function() { 
        choose($(this).text()); 
    }); 
    
    function choose(choice) { 
        user = choice; 
    } 
    

    私はあなたのコードを経て、それを機能させた。

    1. あなたはあまりにも多くのif秒を使用します:私は、次の追加の問題を見たことはほぼ常にをswitch statements
    2. のための良い候補であるMath.randomの値を比較するとき、あなたは((あなたの条件に論理の穴を持っていました))

    以下切り取らは完璧ではありませんが、それは間違いなく改善です:

    var user; 
     
    
     
    $('body').on('click', '.js-choices', function() { 
     
        user = $(this).text(); 
     
        $('#you').html(user); 
     
        $('#computer').html('...'); 
     
        disableButtons(); 
     
    }); 
     
    
     
    
     
    //computer choices 
     
    function computerChoice() { 
     
        var temp = Math.random(); 
     
        if (temp < 0.34) { 
     
        return "rock"; 
     
        } 
     
        if (temp >= 0.34 && temp < 0.67) { 
     
        return "paper"; 
     
        } else { 
     
        return "scissors"; 
     
        } 
     
    } 
     
    
     
    //compare user choice to computer choice 
     
    
     
    // cache your results element to only access it 1 time and improve performance 
     
    // the naming convention says, use a variable with a '$' in front if its 
     
    // a jquery object 
     
    
     
    var $results = $('#results'); 
     
    
     
    function compare(user, computer) { 
     
        if (user === computer) { 
     
        $results.text("How boring, you tied."); 
     
        } else if (user === "rock") { 
     
        if (computer === "scissors") { 
     
         $results.text("They'll feel that in the morning."); 
     
        } else { 
     
         $results.text("Can you breathe? I think not."); 
     
        } 
     
        } else if (user === "scissors") { 
     
        if (computer === "paper") { 
     
         $results.text("Snippitysnip, you sliced them in two."); 
     
        } else { 
     
         $results.text("Ouch, looking a little blunt."); 
     
        } 
     
        } else if (user === "paper") { 
     
        if (computer === "rock") { 
     
         $results.text("You smothered them, eesh!"); 
     
        } else { 
     
         $results.text("You're looking a bit like a banana split."); 
     
        } 
     
        } else { 
     
        $results.text("Something's not quite right...what did you do?!"); 
     
        } 
     
    } 
     
    
     
    // refresh, clear and display computer choices and results. Disable, enable buttons. 
     
    function disableButtons() { 
     
        $('.js-choices').attr('disabled', true); 
     
    } 
     
    
     
    function enableButtons() { 
     
        $('.js-choices').attr('disabled', false); 
     
    } 
     
    
     
    $('#refresh').click(function() { 
     
        $('#results').html(''); 
     
        $('#you, #computer').html('...'); 
     
        computerChoice(); 
     
        enableButtons(); 
     
    }); 
     
    $('.js-play').click(function() { 
     
        compare(user, computerChoice()); 
     
        $('#computer').html(computerChoice); 
     
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
     
    <h1>Rock Paper Scissors</h1> 
     
    <h2>Pick your weapon!</h2> 
     
    <p> 
     
        <button id="refresh">New Game</button> 
     
    </p> 
     
    <button class="js-choices">rock</button> 
     
    <button class="js-choices">paper</button> 
     
    <button class="js-choices">scissors</button> 
     
    <p> 
     
        <button class="js-play">1...2...3...</button> 
     
    </p> 
     
    <p><b id="you">...</b> Vs. 
     
        <b id="computer">...</b></p> 
     
    <p><b id="results"></b></p>

  • +0

    ほとんどのリファクタリングには同意しますが、試行ごとにゲームを結びつける引数なしでcompare関数を呼び出すことができます(私はそれがあなたの見落としであったと思いますが))。確かに – Icepickle

    +0

    :)私はちょうどものを修正していた。 compare()関数は一般的に混乱しています...これらの入れ子のifs .. –

    +0

    それは最初の試み、ブロックスコープ、セミコロンの着実な使用、悪いことではありませんが、改善することができますが、すべての正直で、私は私が考えているところにいくつかの古いコードを覚えておいてください)Btwは、すべてのifの後に結果を一度設定すれば恩恵を受けませんか? :)文を頻繁に繰り返すと、それを除外して一度だけ設定することができます:) – Icepickle

    関連する問題