codewars练习记录2 (js)

【7 kyu】Descending Order

Your task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.
Examples:
Input: 42145 Output: 54421
Input: 145263 Output: 654321
Input: 123456789 Output: 987654321

翻译:
您的任务是生成一个函数,该函数可以接受任何非负整数作为参数,并以降序返回数字。基本上,重新排列数字以创建尽可能高的数字。
示例:
输入:42145输出:54421
输入:145263输出:654321
输入:123456789输出:987654321

解:

function descendingOrder(n){
  return +n.toString().split('').sort((a, b) => b - a).join('')
}
【8 kyu 】Drink about

DESCRIPTION:
Kids drink toddy.
Teens drink coke.
Young adults drink beer.
Adults drink whisky.
Make a function that receive age, and return what they drink.
Rules:
Children under 14 old.
Teens under 18 old.
Young under 21 old.
Adults have 21 or more.

翻译:
孩子们喝toddy。
青少年喝coke。
年轻人喝beer。
成年人喝whisky。
制作一个接收年龄的函数,并返回他们喝的东西。
规则:
14岁以下是儿童。
18岁以下是青少年。
21岁以下是年轻人。
成年人是21岁或更多。

解:

function peopleWithAgeDrink(old) {
  return old < 14 ? 'drink toddy' :
         (old < 18 ? 'drink coke' : 
         (old < 21 ? 'drink beer' : 'drink whisky'));
};
【7 kyu】Find the stray numbe

You are given an odd-length array of integers, in which all of them are the same, except for one single number.
Complete the method which accepts such an array, and returns that single different number.
The input array will always be valid! (odd-length >= 3)
Examples
[1, 1, 2] ==> 2
[17, 17, 3, 17, 17, 17, 17] ==> 3

翻译:
给你一个奇数长度的整数数组,其中所有的整数都是相同的,只有一个数字除外。
完成接受此类数组的方法,并返回单个不同的数字。
输入数组将始终有效!(奇数长度>=3)

解:

// 排序,判断第一个数和第二个数相不相同,不相同取第一个数,相同则取最后一个数
function stray(numbers) {
 let arr = numbers.sort();
 return (arr[0] == arr[1]) ? arr[arr.length-1] : arr[0];
}
【8 kyu 】Twice as old

Your function takes two arguments:
current father’s age (years)
current age of his son (years)
Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old). The answer is always greater or equal to 0, no matter if it was in the past or it is in the future.

翻译:
函数有两个参数:
当前父亲的年龄(岁)
他儿子的当前年龄(岁)
计算出多少年前父亲的年龄是儿子的两倍(或在多少年后他将是儿子的一倍)。答案总是大于或等于0,无论是过去还是将来。

解:

function twiceAsOld(dadYearsOld, sonYearsOld) {

  return Math.abs(dadYearsOld - (sonYearsOld*2));
}
【7 kyu】Maximum Length Difference

You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.

Find max(abs(length(x) − length(y)))

If a1 and/or a2 are empty return -1 in each language except in Haskell (F#) where you will return Nothing (None).

Example:
a1 = [“hoqq”, “bbllkw”, “oox”, “ejjuyyy”, “plmiis”, “xxxzgpsssa”, “xxwwkktt”, “znnnnfqknaz”, “qqquuhii”, “dvvvwz”]
a2 = [“cccooommaaqqoxii”, “gggqaffhhh”, “tttoowwwmmww”]
mxdiflg(a1, a2) --> 13
Bash note:
input : 2 strings with substrings separated by ,
output: number as a string

翻译:
给您两个字符串数组a1和a2。每个字符串由从a到z的字母组成。让x是第一个数组中的任何字符串,y是第二个数组中任何字符串。
查找最大值(abs(长度(x))− 长度(y))
如果a1和/或a2为空,则在每种语言中返回-1,但在Haskell(F#)中除外,在该语言中,您将返回Nothing(None)。
注意:
输入:2个字符串,子字符串由 ’ , ’ 分隔,
输出:数字作为字符串

解:

function mxdiflg(a1, a2) {
  return a1.length==0||a2.length==0 ? -1 : 
         Math.max(
            Math.abs(Math.min(...a2.map(x => x.length)) - Math.max(...a1.map(x => x.length))), 
            Math.abs(Math.max(...a2.map(x => x.length)) - Math.min(...a1.map(x => x.length)))
                 )
}
【8 kyu 】No zeros for heros

Numbers ending with zeros are boring.
They might be fun in your world, but not here.
Get rid of them. Only the ending ones.
1450 -> 145
960000 -> 96
1050 -> 105
-1050 -> -105
Zero alone is fine, don’t worry about it. Poor guy anyway

翻译:
以零结尾的数字很无聊。
他们在你的世界里可能很有趣,但在这里却不是。
摆脱他们。只有结局。
1450 -> 145
960000 -> 96
1050 -> 105
-1050 -> -105
只有零是可以的,不用担心。可怜的家伙。

解:

function noBoringZeros(n) {
  // 利用正则表达式
  return Number(n.toString().replace(/(0+)$/g, ""))
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值