我正在'codewars'网站上尝试Javascript挑战,其中的说明是:
实现一个函数like :: [String] - > String,它必须包含输入数组,包含喜欢项目的人的名字.它必须返回显示文本,如示例所示:
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 more than 4 names, the number in and 2 others simply increases.
Run Code Online (Sandbox Code Playgroud)
我的尝试是这样的:
function likes(names) { …Run Code Online (Sandbox Code Playgroud) 我正在JavaScript学习网站上尝试挑战.
说明是:
决定将"权重"归因于数字.数字的权重从现在开始将是其数字之和例如99将具有"权重"18,100将具有"权重"1因此在列表100中将出现在99之前.给定具有FFC权重的字符串正常顺序的成员你能按照这些数字的"权重"给出这个字符串吗?
示例:a ="56 65 74 100 99 68 86 180 90"按数字重量排序变为:"100 180 90 56 65 74 68 86 99"当两个数字具有相同的"重量"时,让我们将它们分类为好像它们一样字符串而不是数字:100在180之前是因为它的"权重"(1)小于180(9)中的一个而180在90之前,因为它具有相同的"权重"(9)它之前作为字符串.列表中的所有数字都是正数,列表可以为空.
我的尝试如下:
function orderWeight(strng) {
console.log(strng);
if (strng === "") {
return strng;
} else {
var arr = strng.split(" ").sort();
console.log(arr);
var keys = [];
for (var i = 0; i < arr.length; i++) {
var weight = 0;
var current = arr[i];
//loop through the array adding the digits of each number
for (var j = 0; j …Run Code Online (Sandbox Code Playgroud)