Follow these instructions to create a website that displays information about a random emoji.
Click here to view the Emoji Crush Starter project. Copy the project to begin the activity.
There is basically no starter code for this project, but the API will make it relatively simple to add functionality to the site.
The API that makes this possible is named EmojiHub. There are a couple endpoints available, but the important one is the one that returns data about a random emoji. The URL is static, but it will return a variety of random different emoji.
https://emojihub.yurace.pro/api/random
{
"name": "shark",
"category": "animals and nature",
"group": "animal marine",
"htmlCode": [
"🦈"
],
"unicode": [
"U+1F988"
]
}
The important pieces of data are the name
, the category
, and the htmlCode
.
The goal is to generate a random emoji, and add some information to the site when the button is clicked!
To start, it may be worthwhile to look at the HTML on its own, for one example emoji.
Purely for demonstration purposes, add some code to make information about the eye emoji appear.
<div id="emoji-text"></div>
element<p></p>
element that says "You should send them the 👁 emoji
<span></span>
<p></p>
, make another <p></p>
"This emoji is named eye and it is from the smileys and people category"
<span></span>
elements<p></p>
"Sending them this emoji will definitely make them like you"
<i></i>
to make it italicAlternatively to all that, feel free to copy and paste this code into the index.html file:
<p>You should send them the <span>👁</span> emoji</p>
<p>This emoji is named <span>eye</span> and it is from the <span>smileys and people</span> category</p>
<p>Sending them this emoji will <i>definitely</i> make them like you</p>
Run the project to see how it looks!
To start making this more functional, define a function that will dynamically add the HTML to the page.
showEmoji
showEmoji
function, create a variable named textElement
document.querySelector
to grab the <div id="emoji-text">
from the HTML, and store it in the textElement
variabletextElement.innerHTML
property to be a new template literalRun the project, click the “Click Me” button, and verify that information about the eye emoji appears! The added code should look something like this:
function showEmoji() {
let textElement = document.querySelector("#emoji-text");
textElement.innerHTML = `
<p>You should send them the <span>👁</span> emoji</p>
<p>This emoji is named <span>eye</span> and it is from the <span>smileys and people</span> category</p>
<p>Sending them this emoji will <i>definitely</i> make them like you</p>
`;
}
Now this works for one emoji, but it is still very static. The raw HTML contains hard-coded values. Instead, use some dummy data to prepare the showEmoji
function for working with real live data.
The main idea with this is to create some mock data to pretend the API is returning actual data. That way, it will be possible to define how to handle the data even before there is real data! This separation of concerns can be very helpful in software development; breaking large problems into bite-sized tasks can make them feel much less overwhelming. Take it one step at a time!
The example data can be created based on a response from the API. The important thing is that it should be in the same format, and have all the same properties and structure. The shark example from the response above will be used for this dummy data.
exampleData
exampleData
to be an empty objectname
property to the object with a value of "shark"
category
property to the object with a value of "animals and nature"
group
property to the object with a value of "animal marine"
htmlCode
property to the object with a value of ["🦈"]
unicode
property to the object with a value of ["U+1F988"]
The added variable should end up looking something like this:
let exampleData = {
name: "shark",
category: "animals and nature",
group: "animal marine",
htmlCode: ["🦈"],
unicode:["U+1F988"]
};
This simulates the response that would be received from a real fetch
API call.
The next step is for the showEmoji
function to grab that data, find the relevant pieces, and use those pieces in the rendered HTML.
showEmoji
function bodytextElement
variable creationemojiData
, set to exampleData
emoji
emoji
to be the first element from the htmlCode
property of the emojiData
objectname
name
to be the name
property of the emojiData
objectcategory
category
to be the category
property of the emojiData
objectThe added code should look something like this:
let emojiData = exampleData;
let emoji = emojiData.htmlCode[0];
let name = emojiData.name;
let category = emojiData.category;
Now all the data has been extracted, but it still has to be used!
Next, insert that data into the rendered HTML.
showEmoji
functiontextElement.innerHTML
property is set<span>
emoji
variableeye
with the interpolated name
valuecategory
valueRun the project, click the button, and verify that shark emoji information appears! The updated code with the interpolated variables in the template literal should look something like this:
textElement.innerHTML = `
<p>You should send them the <span>${emoji}</span> emoji</p>
<p>This emoji is named <span>${name}</span> and it is from the <span>${category}</span> category</p>
<p>Sending them this emoji will <i>definitely</i> make them like you</p>
`;
}
Now it’s time to get real. Instead of using that dummy data, the code should make a call to the EmojiHub API, and return that data.
The first step is to setup the fetch
call.
fetchEmojiData
async
keywordemojiData
try
/catch
structuretry
block, create a new variable named response
response
to an await
ed call to fetch
https://emojihub.yurace.pro/api/random
try
, set emojiData
to an await
ed response.json()
function callcatch
block, alert the error messagetry
/catch
, at the bottom of the body, return emojiData
The function definition should look something like this:
async function fetchEmojiData() {
let emojiData;
try {
let response = await fetch(`https://emojihub.yurace.pro/api/random`);
emojiData = await response.json();
} catch (e) {
alert(e);
}
return emojiData;
}
Now that the function has been defined, it must be called!
showEmoji
function asynchronous with the async
keywordshowEmoji
function, find the emojiData
variable creationexampleData
with an await
ed call to the fetchEmojiData
functionexampleData
variable from the top of the fileNow, run the project, click the button, and verify that a random emoji appears! The added code for this part should look like this:
let emojiData = await fetchEmojiData();
It is certainly more difficult to build something from scratch, but it also helps reinforce the concepts quite a bit. This API has fairly simple data, but it can be used in powerful and creative ways!
The code in the script.js file should look something like this:
async function fetchEmojiData() {
let emojiData;
try {
let response = await fetch(`https://emojihub.yurace.pro/api/random`);
emojiData = await response.json();
} catch (e) {
alert(e);
}
return emojiData;
}
async function showEmoji() {
let textElement = document.querySelector("#emoji-text");
let emojiData = await fetchEmojiData();
let emoji = emojiData.htmlCode[0];
let name = emojiData.name;
let category = emojiData.category;
textElement.innerHTML = `
<p>You should send them the <span>${emoji}</span> emoji</p>
<p>This emoji is named <span>${name}</span> and it is from the <span>${category}</span> category</p>
<p>Sending them this emoji will <i>definitely</i> make them like you</p>
`;
}
The site is pretty good as-is, but there is always room for improvement. Here are some ideas:
Feel free to use the API for any desired purpose.