2017-11-15 3 views
1

私は初心者です。私はフリーコードキャンプでランダムな見積り機プロジェクトに取り掛かりました。私はランダムな引用符と色を生成することができますが、それらはclickイベントを使用して一度だけ生成されます。それ以降の「新しい見積もり」ボタンをクリックすると、色と引用符の配列の新しいランダム索引が生成されます。クリック機能が配列に複数のランダムなインデックスを適用できないのはなぜですか?

var quote = ["It is not the size of the dog in the fight, but the size of the fight in the dog.-Archie Griffen", "Nothing lasts forever. Not even your troubles.-Arnold H Glasgow", "There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle-Albert Einstein"]; 

// problem area 
var col = ["red", "blue", "yellow", "pink", "brown"]; 
var i = Math.floor(Math.random() * col.length); 
var j = Math.floor(Math.random() * quote.length); 

$(document).ready(function() { 
    $("button").click(function() { 
    $(".quote-window").html(quote[j]); 
    $("body").css("background-color", col[i]); 
    }); 
}); 
+0

まず、適切にあなたの質問をフォーマットするために気をつけてください。コードはコードブロックに入り、引用ブロックには入りません。第二に、誰かがあなたのためにあなたの質問をフォーマットする時間がかかる場合は、それを上書きして、再度それをねじ込むだけではありません。今回は修正しておきます –

+0

すべての行の前に少なくとも4つのスペースを入れて質問の形式を正しく指定してください。現時点では判読不能なので、答えられません。ありがとう! :) –

+0

私はコードをインデントするためにCtrl + Kを使うことができないので、私は電話を使用しています。 – user20490

答えて

1

あなたは<button>クリックハンドラに変数ijを計算する必要があります。

+1

うわー!!今私はそれを得る。ありがとう。あなたは私の一日を作った – user20490

+0

Pls私はもはやこのサイトでもう質問をすることができません。私は6ヶ月間禁止されています。私は私の質問を編集しました。だから私は禁止を解除するためにこの問題についてアップフォーメーションが必要です。 – user20490

1

var quote = ["It is not the size of the dog in the fight, but the size of the fight in the dog.-Archie Griffen", 
 
    "Nothing lasts forever. Not even your troubles.-Arnold H Glasgow", 
 
    "There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle-Albert Einstein" 
 
]; 
 
var col = ["red", "blue", "yellow", "pink", "brown"]; 
 

 
$(document).ready(function() { 
 
    $("button").click(function() { 
 
    var i = Math.floor(Math.random() * col.length); 
 
    var j = Math.floor(Math.random() * quote.length); 
 
    $(".quote-window").html(quote[j]); 
 
    $("body").css("background-color", col[i]); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button>Click Me</button> 
 
<div class='quote-window'></div>

あなたはクリック機能外 ij変数を取得し、それらを変更することはありません。そのため、ボタンをクリックすると、 jiにはすでに値があり、変更されません。

これらの変数宣言をclick関数内に追加します。以下を参照してください

ランダムに生成された数字が以前に生成された数字と同じである可能性があります。しかし、あなたは以下のコードで非常に良い出発点を持っています。

var quote = ["It is not the size of the dog in the fight, but the size of the fight in the dog.-Archie Griffen", "Nothing lasts forever. Not even your troubles.-Arnold H Glasgow", "There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle-Albert Einstein"]; 
 

 
// problem area 
 
var col = ["red", "blue", "yellow", "pink", "brown"]; 
 

 

 

 
$(document).ready(function() { 
 
    $("button").click(function() { 
 
    var i = Math.floor(Math.random() * col.length); 
 
    var j = Math.floor(Math.random() * quote.length); 
 
    $(".quote-window").html(quote[j]); 
 
    $("body").css("background-color", col[i]); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button> 
 
    Click 
 
</button> 
 
<div class="quote-window"> 
 

 
</div>

+0

最初の回答との違いは何ですか? –

+0

@ JasperSeinhorstあなたはこの答えを書くのに30秒かかったと思いますか? –

+0

@JasperSeinhorst私は最初の記事が投稿されている間にこの回答を書いていました。(この時点で)両方の回答が「2分前に回答されました」と表示されていますから –

関連する問題