【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
翻譯:
您的任務(wù)是生成一個(gè)函數(shù),該函數(shù)可以接受任何非負(fù)整數(shù)作為參數(shù),并以降序返回?cái)?shù)字。基本上,重新排列數(shù)字以創(chuàng)建盡可能高的數(shù)字。
示例:
輸入: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。
制作一個(gè)接收年齡的函數(shù),并返回他們喝的東西。
規(guī)則:
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
翻譯:
給你一個(gè)奇數(shù)長(zhǎng)度的整數(shù)數(shù)組,其中所有的整數(shù)都是相同的,只有一個(gè)數(shù)字除外。
完成接受此類數(shù)組的方法,并返回單個(gè)不同的數(shù)字。
輸入數(shù)組將始終有效!(奇數(shù)長(zhǎng)度>=3)
解:
// 排序,判斷第一個(gè)數(shù)和第二個(gè)數(shù)相不相同,不相同取第一個(gè)數(shù),相同則取最后一個(gè)數(shù)
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.
翻譯:
函數(shù)有兩個(gè)參數(shù):
當(dāng)前父親的年齡(歲)
他兒子的當(dāng)前年齡(歲)
計(jì)算出多少年前父親的年齡是兒子的兩倍(或在多少年后他將是兒子的一倍)。答案總是大于或等于0,無(wú)論是過(guò)去還是將來(lái)。
解:
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
翻譯:
給您兩個(gè)字符串?dāng)?shù)組a1和a2。每個(gè)字符串由從a到z的字母組成。讓x是第一個(gè)數(shù)組中的任何字符串,y是第二個(gè)數(shù)組中任何字符串。
查找最大值(abs(長(zhǎng)度(x))? 長(zhǎng)度(y))
如果a1和/或a2為空,則在每種語(yǔ)言中返回-1,但在Haskell(F#)中除外,在該語(yǔ)言中,您將返回Nothing(None)。
注意:
輸入:2個(gè)字符串,子字符串由 ' , ' 分隔,
輸出:數(shù)字作為字符串
解:
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
翻譯:
以零結(jié)尾的數(shù)字很無(wú)聊。
他們?cè)谀愕氖澜缋锟赡芎苡腥ぃ谶@里卻不是。
擺脫他們。只有結(jié)局。
1450 -> 145
960000 -> 96
1050 -> 105
-1050 -> -105
只有零是可以的,不用擔(dān)心??蓱z的家伙。
解:
function noBoringZeros(n) {
// 利用正則表達(dá)式
return Number(n.toString().replace(/(0+)$/g, ""))
}