2017-01-08 5 views
0

これは私の最初のスタックオーバーフローポストです。Javascript - ランダムクォートコードを繰り返さないジェネレータ

私はこれを克服する方法についていくつかの例を試しましたが、修正を見つけることができませんでした(まだ私はJavaScriptの初心者です)。

私はランダムな見積もりジェネレーターを作成しました。これは、「背景を変更し、見積もりを変更する」という15秒ごとまたはクリック時に発生します。

私は現在、コンソールに表示された引用符を記録するために、それ自体を繰り返さないようにしようとしています。

引用符は「配列」内の「オブジェクト」内にあります。

コードは以下のとおりです。あなたの助け ためのおかげで、私は3つのファイル(index.htmlを、quotes.js、script.js) -quotes.js

document.getElementById('loadQuote').addEventListener("click", printQuote, true); 
 

 
//Sets up interval to show print qutoe every 15 seconds 
 
var intervalID = window.setInterval(myCallback, 15000); 
 
function myCallback() { 
 
    printQuote(); 
 
} 
 

 
// Gets a random quote from array Quotes 
 
function getRandomQuote() { 
 
    var pickQuote = quotes[Math.floor(Math.random() * quotes.length)]; 
 
    return pickQuote; 
 
} 
 

 
//prints quote to html 
 
function printQuote() { 
 
    var getQuote = getRandomQuote(); 
 
    var message = ''; 
 
    message += '<p class ="quote">' + getQuote.quote + '</p>'; 
 
    message += '<p class ="source">' + getQuote.source + '</p>'; 
 
    message += '<p class ="tag">' + getQuote.tag + '</p>'; 
 
    document.getElementById('quote-box').innerHTML = message; 
 
    newColor(); 
 
} 
 
// function that will generate random color to the backgroun 
 
var newColor = function randomColor() { 
 
    document.body.style.background = '#'+(Math.random()*0xFFFFFF<<0).toString(16); 
 
}

var quotes = [ 
 
//success quotes 
 
    { 
 
    quote: "If you set your goals ridiculously high and it's a failure, you will fail above everyone else's success.", 
 
    source: "James Cameron", 
 
    tag: "success" 
 
    }, 
 
    { 
 
    quote: "The Way Get Started Is To Quit Talking And Begin Doing", 
 
    source: "Walt Disney", 
 
    tag: "success" 
 
    }, 
 
    { 
 
    quote: "Don’t Let Yesterday Take Up Too Much Of Today.", 
 
    source: "Will Rogers", 
 
    tag: "success" 
 
    }, 
 
    { 
 
    quote: "We Generate Fears While We Sit. We Overcome Them By Action", 
 
    source: "Dr. Henry Link", 
 
    tag: "success" 
 
    }, 
 

 
//health quotes 
 
    { 
 
    quote: "Early to bed and early to rise, makes a man healthy wealthy and wise.", 
 
    source: "Benjamin Franklin", 
 
    tag: "health" 
 
    }, 
 
    { 
 
    quote: "Let food be thy medicine and medicine be thy food.", 
 
    source: "Hippocrates", 
 
    tag: "health" 
 
    }, 
 
    { 
 
    quote: "If you can’t pronounce it, don’t eat it.", 
 
    source: "Common Sense", 
 
    tag: "health" 
 
    }, 
 
    { 
 
    quote: "Health is like money, we never have a true idea of its value until we lose it.", 
 
    source: "Josh Billings", 
 
    tag: "health" 
 
    }, 
 

 
//spirituality quotes 
 
    { 
 
    quote: "Life is really simple, but men insist on making it complicated.", 
 
    source: "Confucius", 
 
    tag: "spirituality" 
 
    }, 
 
    { 
 
    quote: "My religion is very simple. My religion is kindness.", 
 
    source: "Dalai Lama", 
 
    tag: "spirituality" 
 
    }, 
 
    { 
 
    quote: "Knowing others is wisdom; knowing the self is enlightenment.", 
 
    source: "Tao Te Ching", 
 
    tag: "spirituality" 
 
    }, 
 
    { 
 
    quote: "When there is love in your heart, everything outside of you also becomes lovable.", 
 
    source: "Veeresh", 
 
    tag: "spirituality" 
 
    } 
 

 

 
];
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Random Quotes</title> 
 
    <link href='https://fonts.googleapis.com/css?family=Playfair+Display:400,400italic,700,700italic' rel='stylesheet' type='text/css'> 
 
    <link rel="stylesheet" href="css/normalize.css"> 
 
    <link rel="stylesheet" href="css/styles.css"> 
 
</head> 
 
<body> 
 
    <div class="container"> 
 
    <div id="quote-box"> 
 
     <p class="quote">Be the change you wish to see in the world! </p> 
 
     <p class="source">Ghandi </p> 
 
    </div> 
 
    <button id="loadQuote">Show another quote</button> 
 
    </div> 
 
    <script src="js/quotes.js"></script> 
 
    <script src="js/script.js"></script> 
 

 
</body> 
 
</html>

+2

引用符がなくなったときの動作は? –

+0

@DavidEhrmannその場合、 "履歴"をリセットして "サイクル"を再開させるリセット機能が必要になる –

答えて

1

一つを持っていますあなたのgetRandomQuote()機能を少し変更することです:

function getRandomQuote() { 
    return quotes.splice(Math.floor(Math.random() * quotes.length), 1); 
} 

説明:Array.splice()は、配列から項目を削除し、削除された項目を戻します。最初の引数はスプライシングを開始する場所であり、2番目の引数は削除するアイテムの数です。このように使用された引用符は使用時に削除されるので、ページが再ロードされるまでは再び使用することはできません。

+0

非破壊的な方法は、引用配列自体の代わりにインデックスの配列から要素を削除することです。すなわち、 '[ 0,1,2,3、...]となる。 – noisypixy

+0

素晴らしい、魅力のように働いた:)提案のために多くのthx:+1: – nishnash

0

私はあなたのアイデアが好きで、実際にこれらの引用符を読んでいます!おそらく編集を試してみてください:

document.getElementById('loadQuote').addEventListener("click", printQuote, true); 

少し古いかもしれません。試してみてください:

あなたのコードを実行しようとしたとき、私は次のようなエラーメッセージが出ていているため、これを勧める
document.getElementById('loadQuote').onclick = function(name){yourScript}; 

"message": "Uncaught TypeError: Cannot read property 'addEventListener' of null". Just try simplifying.

私はあなたの仕事は自​​分自身初心者と考える人にとっては非常に印象的だと思います!

+0

thx Tha'er、この段階で私は必要な良い小さな動機:) .. @Schlausからの提案は実際には完全に働いた...あなたの助けのためのThx /提案の考え:) – nishnash

関連する問題