В чем ошибка в данном коде?

141
10 января 2020, 15:30

Текст задачи: 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";
    }
}
Answer 1

Вопрос скорее всего закроют из-за опечатки, но я всё же помогу Вам. Вы указали символ присваивания =, если Вы хотите сравнивать без приведения к общему типу, то следуюет писать ===, если же с приведением к общему типу, то ==.

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)

READ ALSO
Изменение значения css свойства

Изменение значения css свойства

Нужно что бы при клике значение translateX изменялось при каждом кликеСейчас событие срабатывает один раз

145
Откуда берутся значения функции

Откуда берутся значения функции

Не пойму, как так получается, что значением аргумента year становятся значения переменных carYear и personYear? Распишите пошагово, пожалуйста

128
Скрыть дочернии элементы при клике

Скрыть дочернии элементы при клике

Нужно при клике скрыть все дочернии элементы класса tp-revslider-slidesli и показать все дочернии элементы класса tp-revslider-slidesli-2, делал так не получилось:

137