Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 7852

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T07:40:08+00:00 2024-11-28T07:40:08+00:00

Day 1: Who likes it? – A coding challenge with solutions

  • 60k

In this weekly series, I will be taking out coding problems from CodeWars and sharing a step-by-step tutorial on how exactly I was able to solve it on my first trial. It is important to keep in mind that my solution may not be in-line with modern practices and techniques, however it is going to be correct. Which is all that really matters lol.

This is an initiative I thought up recently and I hope it helps beginners learn how to program with JavaScript.

So, let's dive in!

Who likes it?

Today's challenge is going to be quite interesting. If you use social media platforms like Facebook, you should by now know of the 'likes' feature where images and posts get liked by users and readers.

In this challenge, we are going to be creating a function which returns different customized messages depending on the number of likers a post gets.

Here are the rules:

likes[]   // "No one likes this" likes["Jack"]     // "Jack likes this" likes["Jack", "Jacob"]      // "Jack and Jacob likes this" likes["Jack", "Jacob", "Jill"]      // "Jack, Jacob and Jill likes this" likes["Jack", "Jacob", "Jill", "John"]      // "Jack, Jacob and 2 others liked this" 
Enter fullscreen mode Exit fullscreen mode

As you can see, the likes() function takes in an array of users who likes a post and returns a different message depending on if one, two, three or four and more users liked the post.

I pulled this test out of a 6 kyu challenge on CodeWars.

Without further ado, let's dig in!

SOLUTION

The first step I always take when solving a coding problem is to break them down into logical steps and representing each of these steps in pseudocode.

STEP 1: CHECKING IF ANYONE LIKED IT

Define the likes function. This function will take in Array of names (strings)
First step to take inside the function is to define an if statement: Check to see if the length of the array is falsy (that is, the array is empty and no one liked the post).

If it is empty, return a string with the meaage which says “No one likes this post”

function likes ( ...names ) {   if( !names.length) {      return "No one likes this";  }  // Continuation 
Enter fullscreen mode Exit fullscreen mode

STEP 2: LOOPING OVER THE ARRAY AND STORING NUMBER OF LIKERS

If we got to this point, it means that there is atleast one name present in the Array. Create a count variable and set its value to zero. After you are done with that, loop through the list of name. For every iteration you make, increment the value of count by one.

 let count = 0; names.forEach(name => count++); 
Enter fullscreen mode Exit fullscreen mode

STEP 3: CHECKING TO SEE HOW MANY LIKED

Step 2 was about looping through the array and increasing the count by one for every likers encountered.

Now, we are going to implement a chain of conditional statements which is geared towards returning a new message for every number of likers.

First statement checks if the count variable is one, which means that one person liked the post. If true, we will get the name of the sole liker and return the following message: insert_liker_name likes this post

Second statement checks if the count variable is two, which means that two people liked the post. If true, we will get the name of the two likers and return the following message: liker_1 and liker_2 likes this post

Third statement checks if the count variable is three, which means that three people liked the post. If true, we will get the name of the three likers and return the following message: liker_1, liker_2 and liker_3 likes this post

The fourth and final statement checks if the count variable is four or above, which means that at least four people liked the post. If true, we will first subtract two (i.e the people who will be displayed) from the number of likers, that is count. Then we will get the first two names from the list of likers and return the following message: liker_1, liker_2 and remaining_numbers likes this post

if(count == 1) {     const firstName = names[0];     return `${firstName} likes this post`   } else if (count == 2) {     const firstName = names[0]     const secondName = names[1]     return `${firstName} and ${secondName} likes this post`   }   else if (count == 3) {     const firstName = names[0]     const secondName = names[1]     const thirdName = names[2]     return `${firstName}, ${secondName} and ${thirdName} likes this post`   } else {     const remainder = count - 2;     const firstName = names[0]     const secondName = names[1]     return `${firstName}, ${secondName} and ${remainder} others likes this post`   }  }    
Enter fullscreen mode Exit fullscreen mode

Now, lets see the full program:

function likes(...names) {   if(!names.length) {     return "No one likes this";   }    let count = 0;   names.forEach(name => count++);    if(count == 1) {     const firstName = names[0];     return `${firstName} likes this post`   } else if (count == 2) {     const firstName = names[0]     const secondName = names[1]     return `${firstName} and ${secondName} likes this post`   }   else if (count == 3) {     const firstName = names[0]     const secondName = names[1]     const thirdName = names[2]     return `${firstName}, ${secondName} and ${thirdName} likes this post`   } else {     const remainder = count - 2;     const firstName = names[0]     const secondName = names[1]     return `${firstName}, ${secondName} and ${remainder} others likes this post`   }  }  const likers = ["Jack", "Jill"]  console.log(likes(...likers)); 
Enter fullscreen mode Exit fullscreen mode

RESULT

JSFiddle-Code-Playground.png

JSFiddle-Code-Playground (1).png

This simple challenge was really fun for me on the first trial and I hope it was the same for you. You can copy the code and test it yourself on JS Fiddle.

If you have a better way of solving this problem, please put it down in the comments. I'd love to check it out. If you have any suggestions, I'd love to hear it!

I will be doing this every Mondays, Wednesdays and Fridays. Follow/Subscribe to this blog to be updated. I will be tackling a new challenge in public on Friday.

Until then, friends!

P/S: If you are learning JavaScript, I recently created an eBook which teaches 50 topics in hand-written notes. Check it out here

beginnersjavascriptprogrammingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.