Текст задачи: 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 a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in the examples:
likes [] // must be "no one likes this"
likes ["Peter"] // must be "Peter likes this"
likes ["Jacob", "Alex"] // must be "Jacob and Alex like this"
likes ["Max", "John", "Mark"] // must be "Max, John and Mark like this"
likes ["Alex", "Jacob", "Mark", "Max"] // must be "Alex, Jacob and 2 others like this"
For 4 or more names, the number in and 2 others simply increases.
Мой код-решение:
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 if (names.length > 3) {
return names[0] + ", " + names[1] + " and " + (names.length - 2) + " others like this";
}
}
Вопрос скорее всего закроют из-за опечатки, но я всё же помогу Вам. Вы указали символ присваивания =
, если Вы хотите сравнивать без приведения к общему типу, то следуюет писать ===
, если же с приведением к общему типу, то ==
.
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 if (names.length > 3) {
return names[0] + ", " + names[1] + " and " + (names.length - 2) + " others like this";
}
}
// var array = [];
// var array = ["Peter"];
var array = ["Jacob", "Alex"]
// var array = ["Max", "John", "Mark"]
// var array = ["Alex", "Jacob", "Mark", "Max"]
var response = likes(array);
console.log(response)
Кофе для программистов: как напиток влияет на продуктивность кодеров?
Рекламные вывески: как привлечь внимание и увеличить продажи
Стратегії та тренди в SMM - Технології, що формують майбутнє сьогодні
Выделенный сервер, что это, для чего нужен и какие характеристики важны?
Современные решения для бизнеса: как облачные и виртуальные технологии меняют рынок
Нужно что бы при клике значение translateX изменялось при каждом кликеСейчас событие срабатывает один раз
Не пойму, как так получается, что значением аргумента year становятся значения переменных carYear и personYear? Распишите пошагово, пожалуйста
Нужно при клике скрыть все дочернии элементы класса tp-revslider-slidesli и показать все дочернии элементы класса tp-revslider-slidesli-2, делал так не получилось: