Explorer
project
index.html
index.js
Dependencies
Bootcamp
Study group
Collaborate with peers in your dedicated #study-group channel.
Code reviews
Submit projects for review using the /review
command in your #code-reviews channel
/* Emojify!
Popular services like Slack and Github allow for emoji shortscodes, meaning
they will detect when a word in a sentence begins and ends with a colon (:)
and automatically replace that word with an emoji.
These shortcodes allow users to add an emoji to their messages by typing a
code rather than searching for an emoji from a list.
For example, typing :smile: will replace that text with 😊
*/
const emojis = {
"smile": "😊",
"angry": "😠",
"party": "🎉",
"heart": "💜",
"cat": "🐱",
"dog": "🐕"
}
/* 1. Write a function that checks if a lowercase word starts and
ends with a colon. If it does, remove the colons and
look up the word in the emoji object. If the word is in the
emojis object, return the corresponding emoji.
If it isn't, return the original word.
Example input: ":party:"
Example output: 🎉
Example input: ":flower:"
Example output: "flower"
Example input: "elephant"
Example output: "elephant"
*/
// First challenge function
function emojifyWord(word){
const emojisArray = Object.entries(emojis);
if(word.startsWith(':') && word.endsWith(':')) {
const wordSlice = word.slice(1,-1)
for(let i = 0; i < emojisArray.length; i++) {
if(wordSlice === emojisArray[i][0]) {
return emojisArray[i][1];
}
}
return wordSlice;
}
return word;
}
// Second challenge function
function emojifyPhrase(phrase){
const phraseArray = phrase.split(' ');
for(let i = 0; i < phraseArray.length; i++) {
if(phraseArray[i].startsWith(':') && phraseArray[i].endsWith(':')) {
phraseArray.splice(i,1,emojifyWord(phraseArray[i]));
}
}
return phraseArray.join(' ');
}
console.log(emojifyWord(":Heart:"));
console.log(emojifyWord(":flower:"));
console.log(emojifyWord("elephant"));
console.log(emojifyWord(":cat:"));
console.log(emojifyPhrase("I :heart: my :cat:"));
console.log(emojifyPhrase("I :heart: my :elephant:"));