[solved] How do I convert a text to an emoji? | SoloLearn: Learn to code for FREE!

+16

[solved] How do I convert a text to an emoji?

I wanted to make a program that collects a property from an object and checks if that property matches a character or a word in a given sentence, if true then the program replaces that property with it's corresponding value. e.g obj = { ":)" : "🙂", ":(" : "☹️" } str = "hello friend :)" I want my output to be hello friend 🙂 I have tried solving this but my code returns no output. please help. my code [produces no output] https://code.sololearn.com/WRkJLPpEozU4/?ref=app

1/10/2021 4:06:55 AM

Stonny

22 Answers

New Answer

+21

Stonny emoji is an object not array so you have to do if(j == words[i]) edited: if (j == words[i]) { words[i] = emoji[j]; } https://code.sololearn.com/WOK0vpTLdmbB/?ref=app

+6

Hi Stonny , 🙂 Hope this will help 🐾 https://code.sololearn.com/WFMM50G070Hb/?ref=app

+6

I also don't know about thanks for questioning I got my Answer 💓

+3

Ipang I don't really understand the code. I tried replacing the 0 and 1 with emojis but got confused in line 21

+3

Stonny You are joining the word inside the condition so when condition will match then it will be append in words otherwise not that's why you got only :)

+3

I Am AJ ! Thanks your solution, it works. It is what I want. [solved]https://code.sololearn.com/WOK0vpTLdmbB/?ref=app

+3

Stonny Arnav Kumar [📕Schools📚] There are many ways to achieve this. You can also do like this: for(i in words) { if (emoji [words[i]]) //it will return true if exist words[i] = emoji [words[i]]; } words = words.join(" ")

+3

Another way of using replaceAll method. Get all the key elements into an array using Object.keys method, then iterate replaceAll method with respected key and value. You only need this: Object.keys(emoji).forEach(key => words = words.replaceAll(key, emoji[key])); https://code.sololearn.com/WOkgBCC1bmWq/?ref=app

+2

I wrote this to answer a friend just the other day. If you comment line 47 and add the emojis to the <sym> object, I think you can have just what you want. https://code.sololearn.com/WV2nFIAbBtsf/?ref=app

+2

Stonny Check my edited code again

+2

Stonny You missed one more thing in your code for (j in emoji) //here j but you wrote emoji [i] there should be emoji [j]

+2

https://code.sololearn.com/WBdSTxhBt9Rl/?ref=app Fixed

+2

Thanks so much Ipang it really helps :)

+2

Arnav Kumar [📕Schools📚] I really appreciate your own version. I'm learning so much from this exercise :) thanks so much

+2

Narayan Tirvir I suggest you post your question direct on your Q&A page so everyone can see it.

+2

Narayan Tirvir You are asking at wrong place.

+1

I Am AJ ! I just did that now and got ':)' as the only output

+1

Stonny The code was meant to replace characters in each string in an array <maps>. A bit of adjustment will get you right.

+1

Thanks so much P∆WAN M∆URY∆ It works perfect 🤩

+1

https://code.sololearn.com/WiI7Ye3x6PH9/?ref=app