Skip to content

Instantly share code, notes, and snippets.

@cmac4603
Last active March 21, 2016 13:50
Show Gist options
  • Save cmac4603/605995dbf09807c83384 to your computer and use it in GitHub Desktop.
Save cmac4603/605995dbf09807c83384 to your computer and use it in GitHub Desktop.
fCC: Build A Random Quote Machine

fCC: Build A Random Quote Machine

Objective: Build a CodePen.io app that is functionally similar to this: http://codepen.io/FreeCodeCamp/full/bELoPJ.
Rule #1: Don't look at the example project's code. Figure it out for yourself.
Rule #2: Fulfill the below user stories. Use whichever libraries or APIs you need. Give it your own personal style.
User Story: I can click a button to show me a new random quote.
User Story: I can press a button to tweet out a quote.
Remember to use Read-Search-Ask if you get stuck.
When you are finished, click the "I've completed this challenge" button and include a link to your CodePen. 
You can get feedback on your project from fellow campers by sharing it in our Code Review Chatroom. You can also share it on Twitter and your city's Campsite (on Facebook).

A Pen by Colin MacRae on CodePen.

License.

<body>
<div class="main">
<div class="container">
<div id="theQuote" class="quote">
</div>
<div>
<button id="twitterButton" class="btn" onclick="tweetIt()"><i class="fa fa-twitter"></i></button>
<button id="getQuote" class="btn btn-primary">
Get New Fact
</button>
</div>
</div>
<div id="author" class="author">
<p>Brought to you by <a href="https://github.com/cmac4603/">cmac4603</a><p>
</div>
</div>
</div>
</body>
$(document).ready(function() {
$.getJSON("http://api.icndb.com/jokes/random", function(json) {
$(".quote").html(JSON.stringify(json.value.joke));
var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$('body').css("background-color", hue).css("color", hue);
$('.btn').css("background-color", hue);
});
});
$(document).ready(function() {
$("#getQuote").on("click", function(){
$.getJSON("http://api.icndb.com/jokes/random?exclude=[nerdy]", function(json) {
$(function() {
$(".quote").fadeOut(800, function() {
$(".quote").html(JSON.stringify(json.value.joke)).fadeIn(800);
});
});
var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$('body').animate({backgroundColor: hue}, {duration: 1000, queue: false}).animate({color: hue}, {duration: 1000, queue: false});
$('.btn').animate({backgroundColor: hue}, {duration: 1000, queue: false})
});
});
});
function tweetIt () {
var phrase = document.getElementById('theQuote').innerText;
if (phrase.length > 80) {
phrase = phrase.slice(0, 80) + '..." Finish at:';
};
var tweetUrl = 'https://twitter.com/share?text=' +
encodeURIComponent(phrase)
+ '&url=' +
'http://www.icndb.com/';
window.open(tweetUrl);
};
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
body {
margin: 0;
padding: 0;
}
.main {
margin: 150px 60px;
}
.container {
border-radius: 3px;
background-color: white;
max-width: 650px;
}
.quote {
padding: 20px;
font-size: 24px;
font-weight: 600;
font-family: 'Indie flower', cursive;
text-align: center;
}
.btn {
font-family: 'Noto Sans', sans-serif;
border: none;
margin-bottom: 10px;
}
#twitter-share-button {
float: left;
}
.author {
color: white;
text-align: center;
padding: 10px;
}
a {
text-decoration: none;
color: white;
}
#twitterButton {
color: white;
padding: 3px 8px;
font-size: 20px;
}
#getQuote {
float: right;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Indie+Flower|Noto+Sans" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment