2017-09-22 6 views
-2

私はFree Code Camp's "Random Quote Machine"を使って作業していますが、私は非常に(非常に)重視したモデルを作りましたが、引用符をつけて自分のコードの方法でつぶす方法はわかりません。私は現在のJavascriptコードを変更する必要がありますか?それとも私の現在のコードで私が気づいていないつぶやきを送信する方法はありますか?私は解決策があるanother question like thisがあることを知っていますが、私はそれを理解しませんでした。本当にありがとう。ランダム引用機械、つぶやき引用

var quotes= [ 
 
    'I love you the more in that I believe you had liked me for my own sake 
 
    and for nothing else. John Keats', 
 
    'But man is not made for defeat. A man can be destroyed but 
 
    not defeated. Ernest Hemingway', 
 
    'When you reach the end of your rope, tie a knot in it and 
 
    hang on. Franklin D. Roosevelt', 
 
    'Always do what is right. It will gratify half of mankind 
 
    and astound the other. ― Mark Twain', 
 
    'Whenever you find yourself on the side of the majority, it 
 
    is time to pause and reflect. Mark Twain', 
 
    'It is curious that physical courage should be so common in 
 
    the world, and moral courage so rare.- Mark Twain in Eruption', 
 
    'A man should not be without morals; it is better to have 
 
    bad morals than none at all.- Mark Twain\'s Notebook', 
 
    'The most permanent lessons in morals are those which come, 
 
    not of book teaching, but of experience.- A Tramp Abroad', 
 
    'But the fact that he can do wrong proves his moral 
 
    inferiority to any creatures that cannot.― Mark Twain', 
 
    '\“Wrong does not cease to be wrong because the majority 
 
    share in it.\” ― Leo Tolstoy', 
 
    '\“Right is right even if no one is doing it; wrong is wrong 
 
    even if everyone is doing it.\” ― Augustine of Hippo' 
 
] 
 
function nextQuote() { 
 
    var randomNumber = Math.floor(Math.random()*quotes.length); 
 
    document.getElementById("displayQuote").innerHTML = quotes[randomNumber]; 
 
}
<h1>Random Quote Machine</h1> 
 
<!--Javascript will go in div--> 
 
<div> 
 
    <h2 id="displayQuote"></h2> 
 
</div> 
 
<button onclick="nextQuote()">New Quote</button> 
 
<button id="tweetQuote"><a>Tweet</a></button>

答えて

0

あなたquotes -array要素にラインブレーキを持っていなければならないので、あなたのコードは、「終端されていない文字列leteral」のようなエラーがスローされます。

早くクリーンアップした後は、チャームのように機能します。私の最初の答えは、元のソースコードを修正についてでしたが

あなたは何かが動作しない場合は、あなたのコードをデバッグする必要が

var quotes= [ 
 
    'I love you the more in that I believe you had liked me for my own sake and for nothing else. John Keats', 
 
    'But man is not made for defeat. A man can be destroyed but not defeated. Ernest Hemingway', 
 
    'When you reach the end of your rope, tie a knot in it and hang on. Franklin D. Roosevelt', 
 
    'Always do what is right. It will gratify half of mankind and astound the other. ― Mark Twain', 
 
    'Whenever you find yourself on the side of the majority, it is time to pause and reflect. Mark Twain', 
 
    'It is curious that physical courage should be so common in the world, and moral courage so rare.- Mark Twain in Eruption', 
 
    'A man should not be without morals; it is better to have bad morals than none at all.- Mark Twain\'s Notebook', 
 
    'The most permanent lessons in morals are those which come, not of book teaching, but of experience.- A Tramp Abroad', 
 
    'But the fact that he can do wrong proves his moral inferiority to any creatures that cannot.― Mark Twain', 
 
    '\“Wrong does not cease to be wrong because the majority share in it.\” ― Leo Tolstoy', 
 
    '\“Right is right even if no one is doing it; wrong is wrong even if everyone is doing it.\” ― Augustine of Hippo' 
 
] 
 
function nextQuote() { 
 
    var randomNumber = Math.floor(Math.random()*quotes.length); 
 
    document.getElementById("displayQuote").innerHTML = quotes[randomNumber]; 
 
    document.getElementById("tweetQuote").href="https://twitter.com/intent/tweet/?text=" + quotes[randomNumber]; 
 
}
<h1>Random Quote Machine</h1> 
 
<!--Javascript will go in div--> 
 
<div> 
 
    <h2 id="displayQuote"></h2> 
 
</div> 
 
<button onclick="nextQuote()">New Quote</button> 
 
<a href="#" id="tweetQuote"><button>Tweet</button></a>
...

+0

私はそれが非常に高速である、それは改行を持っていないので、あなたは正しいです、それを作りました。そこから見積もりをつぶす方法を知っていますか?私はこの投稿(https://stackoverflow.com/questions/45172377/twitter-share-button-with-random-quote?rq=1)に行ってきましたが、addEventListenerを追加することによって、彼らが何をしているのか分かりませんでした。 (私はW3の学校に行った)私はこれが初心者の質問であることを知っている、あなたの忍耐のためにありがとう –

+0

@JohnSmith注意:私のコードにバグがありました。今、そのupdatetと大丈夫... – Axel

+0

残念ながら、私はまだ15人の担当者を持っていない、 "それは記録されますが、公に追加されていません。私が15歳になると、私は戻って、投票ボタンを再度押してください。あなたの善行は気づかれません。ご協力ありがとうございました。 –

0

が、これはより多くのようなものですより良い方法で機能を実装する方法に関する推奨事項。

// lets start with a "Immediately Invoked Function Expression" 
 
// to not pollute the global namespace 
 
(function() { 
 

 
    // cache elements that are reused 
 
    var aQuotes = [ 'Quote 1', 'Quote 2', 'Quote 3', 'Quote 4' ], 
 
     iQuotes = aQuotes.length, 
 
     domDisplayQuote = document.getElementById("displayQuote"), 
 
     domTweetQuote = document.getElementById("tweetQuote"); 
 

 
    // listen to "click event" on "button#nextQuote" 
 
    document.getElementById("nextQuote").addEventListener('click', function() { 
 

 
     var sQuote = aQuotes[ Math.floor(Math.random() * iQuotes) ]; 
 
     // use cached DOMelements 
 
     domDisplayQuote.innerHTML = sQuote; 
 
     domTweetQuote.href="https://twitter.com/intent/tweet/?text=" + sQuote; 
 

 
    }), false; 
 

 
})()
<h1>Random Quote Machine</h1> 
 
<div> 
 
    <h2 id="displayQuote"></h2> 
 
</div> 
 
<!-- instead of <button onclick="nextQuote()">New Quote</button> --> 
 
<button id="nextQuote">New Quote</button> 
 
<a href="#" id="tweetQuote"><button>Tweet</button></a>