- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Displaying likes on a post wherein array specifies the names of people that liked a particular post using JavaScript
Problem
We are required to write a JavaScript function that takes in an array of names (string). This array specifies the names of people that liked a particular post on some social networking site.
If the count of likes are less than or equal to three our function should simply return all names saying these people liked the post but if the count is greater than three then our function should return first two names and remaining count.
Example
Following is the code −
const names = ['Ram', 'Manohar', 'Jay', 'Kumar', 'Vishal']; const displayLikes = (names) => { return [ 'no one likes this', `${names[0]} likes this`, `${names[0]} and ${names[1]} like this`, `${names[0]}, ${names[1]} and ${names[2]} like this`, `${names[0]}, ${names[1]} and ${names.length - 2} others like this`, ][ Math.min(4, names.length) ]; }; console.log(displayLikes(names));
Output
Ram, Manohar and 3 others like this
- Related Articles
- Engage a team of people and improve a blog post
- Post favourite stuffs using pixelpumper on mac
- Post a flow chart of Nephron.
- Using POST Methods in Perl
- Passing Information Using POST Method in Python
- Using post request in middleware in express
- GET and POST requests using Python Programming
- A ( 1.6 mathrm{~m} ) tall girl stands at a distance of ( 3.2 mathrm{~m} ) from a lamp-post and casts a shadow of ( 4.8 ) m on the ground. Find the height of the lamp-post by using trigonometric ratios.
- Cyber Security in a Post-COVID World
- How to make an HTTP POST request on iOS App using Swift?
- POST unchecked HTML checkboxes
- How to create a POST request in Postman?
- PHP – Parse the GET, POST, and COOKIE data using mb_parse_str()
- Explain the concept of Post-Merger Integration (PMI)
- Post-Traumatic Stress Disorder (PTSD)

Advertisements