Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Counting emojis for users with constraints

vlad

New Coder
Hi everyone,
I’m working on a JavaScript task and can't find a solution that satisfies all the given constraints.

Task:​

Write a function countEmoji(message, emoji) that counts the occurrences of a specific emoji for each mentioned user in a message.

Constraints:​

  1. Use only one loop (and it must be for loop).
  2. Use one object to store the results.
  3. Use charCodeAt to identify specific characters and fromCharCode for character conversion to lowercase.
  4. No built-in methods for strings or arrays (e.g., split, match, forEach, etc.).

Additional Rule:​

If there’s no valid emoji patern with needed emoji between two user mentions, group the users and count the next valid emojis for all grouped users.
Test cases:

JavaScript:
const text = '<@Kate />:apple: <@Max/>:like:<@alisa /> :like: received:apple::apple:'

countEmoji(text, 'apple');
//expected result
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:apple: :APPLE: :AppLe:<@alisa /> :like: received:apple::apple:'
//{
//kate: 1,
//max: 3,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:apple APPLE: AppLe:<@alisa /> :like: received:apple::apple:'
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:like:<@alisa /> :like: received:apple::apple: <@kate / > alsdaksdjhsa <@KATE / > :apple: :apple:'
//{
//kate: 3,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/><@olia/><@misha/><@dasha/><@alisa /> :like: received:apple::apple:  <@dima/><@vasia/><@gena/><@ihor/><@tolik />'
//{
//kate: 1,
//max: 2,
//alisa: 2,
//olia:2,
//misha:2,
//dasha:2,
//dima:0,
//vasia:0,
//gena:0,
//ihor:0,
//tolik:0
//}

//const text = ':apple: :apple: <@Kate />:apple: <@Max/><@alisa /> :like: received:apple::apple: '
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

Challenges Faced:​

I’ve tried various approaches, but either I exceed the constraints (e.g., using multiple loops or arrays with methods like forEach) or fail to handle all edge cases.

If anyone has advice, a direction, or a working example that respects the constraints, I’d really appreciate the help!
 
Last edited:
Hi everyone,
I’m working on a JavaScript task and can't find a solution that satisfies all the given constraints.

Task:​

Write a function countEmoji(message, emoji) that counts the occurrences of a specific emoji for each mentioned user in a message.

Constraints:​

  1. Use only one loop (and it must be for loop).
  2. Use one object to store the results.
  3. Use charCodeAt to identify specific characters and fromCharCode for character conversion to lowercase.
  4. No built-in methods for strings or arrays (e.g., split, match, forEach, etc.).

Additional Rule:​

If there’s no valid emoji patern with needed emoji between two user mentions, group the users and count the next valid emojis for all grouped users.
Test cases:

JavaScript:
const text = '<@Kate />:apple: <@Max/>:like:<@alisa /> :like: received:apple::apple:'

countEmoji(text, 'apple');
//expected result
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:apple: :APPLE: :AppLe:<@alisa /> :like: received:apple::apple:'
//{
//kate: 1,
//max: 3,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:apple APPLE: AppLe:<@alisa /> :like: received:apple::apple:'
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/>:like:<@alisa /> :like: received:apple::apple: <@kate / > alsdaksdjhsa <@KATE / > :apple: :apple:'
//{
//kate: 3,
//max: 2,
//alisa: 2
//}

//const text = '<@Kate />:apple: <@Max/><@olia/><@misha/><@dasha/><@alisa /> :like: received:apple::apple:  <@dima/><@vasia/><@gena/><@ihor/><@tolik />'
//{
//kate: 1,
//max: 2,
//alisa: 2,
//olia:2,
//misha:2,
//dasha:2,
//dima:0,
//vasia:0,
//gena:0,
//ihor:0,
//tolik:0
//}

//const text = ':apple: :apple: <@Kate />:apple: <@Max/><@alisa /> :like: received:apple::apple: '
//{
//kate: 1,
//max: 2,
//alisa: 2
//}

Challenges Faced:​

I’ve tried various approaches, but either I exceed the constraints (e.g., using multiple loops or arrays with methods like forEach) or fail to handle all edge cases.

If anyone has advice, a direction, or a working example that respects the constraints, I’d really appreciate the help!
Hi there,
What have you tried so far? What does your code look like at the moment?
 
Hi there,
What have you tried so far? What does your code look like at the moment?
This is the latest version that works for every test case, but it uses .push, .find and .forEach:

JavaScript:
function countEmoji(message, emoji) {
    const result = {}

    let name = '';
    let activeNames =[];
    let collectingName = false;

    let currentEmoji = '';
    let activeEmoji= 0;
    let collectingEmoji = false;
    
    for (let i=0; i< message.length; i++){
        const charCode = message.charCodeAt(i);
        if(charCode ===64){
            if(activeEmoji > 0){
                activeNames.forEach(activeName => {
                    result[activeName] = activeEmoji;
                });
                activeNames = [];
                activeEmoji = 0;
                name = '';
                collectingName=true;
            }
            
            else{
                name = '';
                collectingName=true;
            } 
        }
        else if(collectingName) {
            
            if(message.charCodeAt(i)>=65&&message.charCodeAt(i)<=90){
                name+= String.fromCharCode(message.charCodeAt(i)+32)
            }else {
                name += message[i];
            }
            
            if(message.charCodeAt(i+1)===32|| message.charCodeAt(i+1)===47){
                collectingName=false;
                if (!activeNames.find(existingName => existingName === name)) {
                    activeNames.push(name);
                }       
            }
            
        }

        if (charCode === 58) {
            if (!collectingEmoji) {
                collectingEmoji = true;
                currentEmoji = '';
            }
        } else if (collectingEmoji) {
            if(message.charCodeAt(i)>=65&&message.charCodeAt(i)<=90){
                currentEmoji+= String.fromCharCode(message.charCodeAt(i)+32)
            }else {
                currentEmoji += message[i];
            }
            if (message.charCodeAt(i + 1) === 58) {
                collectingEmoji = false;

                if (currentEmoji === emoji) {
                    activeEmoji++
                }
            }
        }
        
    }

    if(activeNames.length!== 0){
        if(activeEmoji > 0){
            // console.log(activeNames)

            activeNames.forEach(activeName => {
                result[activeName] = (result[activeName] || 0)+activeEmoji;
            });
        } else{
            activeNames.forEach(activeName => {
                result[activeName] = 0;
            });
        }
    }
    return result
}
 
You violate constraint #4 here... so what you can do here is break this off into its own function and use a for loop
I really appreciate the time you've dedicated, but I already know that I'm violating the conditions here, as I mentioned in my first message:
" I’ve tried various approaches, but either I exceed the constraints (e.g., using multiple loops or arrays with methods like forEach) or fail to handle all edge cases."
That’s why I asked for help, because I don’t know how to complete the task without this. Regarding the advice to use a for loop, the first condition of this task is to use only one loop.
 
firstly we will use a single for loop to traverse the string and keep an object to store results while we are gonna use two methods to handle case insensitivity then you can manage groups of users and count emojis accordingly
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom