Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Who likes it

/*
// You probably know the "like" system from Facebook and other pages. 
People can "like" blog posts, pictures or other items. We want to create the text 
that should be displayed next to such an item.

// Implement the function which takes an array containing the names of people that 
like an item. It must return the display text as shown in the examples:

  []                                -->  "no one likes this"
  ["Peter"]                         -->  "Peter likes this"
  ["Jacob", "Alex"]                 -->  "Jacob and Alex like this"
  ["Max", "John", "Mark"]           -->  "Max, John and Mark like this"
  ["Alex", "Jacob", "Mark", "Max"]  -->  "Alex, Jacob and 2 others like this"
  Note: For 4 or more names, the number in "and 2 others" simply increases.
*/

function likes(names) {
  let likeText = ["this"], arrLength = names.length
  
  if(arrLength <= 0) likeText.unshift("no one likes")
  else if(arrLength <= 1) likeText.unshift(`${names} likes`)
  else if(arrLength <= 3) likeText.unshift(names.slice(0, -1).join(", "), `and ${names[arrLength - 1]} like`)
  else likeText.unshift(names.slice(0, 2).join(", "), `and ${arrLength - 2} others like`)

  return likeText.join(' ').toString()
}

// With love @kouqhar
Comment

Who likes it?

function likes(names) {



    if (names.length === 0) {
        return 'no one likes this'
    }
    else if (names.length === 1) {
        return `${names[0]} likes this`
    }

    else if (names.length === 2) {
        return `${names[0]} and ${names[1]} like this`
    }

    else if (names.length === 3) {
        return `${names[0]}, ${names[1]} and ${names[2]} like this`

    }

    else {
        let pepole = names.length - 2;
        return `${names[0]}, ${names[1]} and ${pepole} others like this`
    }


}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Exercice âge JavaScript 
Javascript :: reactjs doc error 
Javascript :: remove text and keep div inside a div jquery 
Javascript :: how to invoke a function in a class 
Javascript :: object destructuring in javascript 
Javascript :: Expresion regular para validar nombres de usuario 
Javascript :: check notification permissopn allow or not 
Javascript :: Call this API in order to fetch the user data. API: https://jsonplaceholder.typicode.com/users. 
Javascript :: function Tom(a, b) { return a + b; } 
Javascript :: sadd in redis 
Javascript :: Adding A Property To BuiltIn Class In Javascript 
Javascript :: convert javascript to python 
Javascript :: react native version 
Javascript :: redux-persist 
Javascript :: js brightness filter 
Javascript :: how to assign an rest operator in javascript 
Javascript :: how to use array of object in react 
Javascript :: setimmediate node example 
Javascript :: react native scrollview item bottom 
Javascript :: first element of array js 
Javascript :: mongoose update array push multiple 
Javascript :: install tailwind css with next js 
Javascript :: js code 
Javascript :: splice state react 
Javascript :: check cookie existence js 
Javascript :: update text react native 
Javascript :: Using the Set object 
Javascript :: python to java script 
Javascript :: how do you calculate what percentage a number is of another number 
Javascript :: how to wait for the subscribe to finish before going back to caller method in angular 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =