22 Answers
New AnswerI 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
Stonny22 Answers
New AnswerStonny 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
Hi Stonny , 🙂 Hope this will help 🐾 https://code.sololearn.com/WFMM50G070Hb/?ref=app
Ipang I don't really understand the code. I tried replacing the 0 and 1 with emojis but got confused in line 21
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 :)
I Am AJ ! Thanks your solution, it works. It is what I want. [solved]https://code.sololearn.com/WOK0vpTLdmbB/?ref=app
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(" ")
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
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
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]
Arnav Kumar [📕Schools📚] I really appreciate your own version. I'm learning so much from this exercise :) thanks so much
Narayan Tirvir I suggest you post your question direct on your Q&A page so everyone can see it.
Stonny The code was meant to replace characters in each string in an array <maps>. A bit of adjustment will get you right.
Learn Playing. Play Learning
SoloLearn Inc.
4 Embarcadero Center, Suite 1455Send us a message