2017-09-22 98 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

你的代碼拋出類似「未終止的字符串leteral」的錯誤,因爲你不能在你的quotes - 陣列元素線剎車。

經過快速清理 - 現在它就像一個魅力。

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。 (我訪問過W3schools)我知道這是一個初學者的問題,感謝您的耐心等待 –

+0

@JohnSmith請注意:我的代碼中存在一個錯誤。現在它的更新和好... – 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>