[8 kyu] UEFA EURO 2016
Finish the uefaEuro2016() function so it return string just like in the examples below:
uefaEuro2016(['Germany', 'Ukraine'],[2, 0]) // "At match Germany - Ukraine, Germany won!"
uefaEuro2016(['Belgium', 'Italy'],[0, 2]) // "At match Belgium - Italy, Italy won!"
uefaEuro2016(['Portugal', 'Iceland'],[1, 1]) // "At match Portugal - Iceland, teams played draw."
解:
function uefaEuro2016(teams, scores){
if(scores[0] > scores[1]) return `At match ${teams[0]} - ${teams[1]}, ${teams[0]} won!`;
if(scores[0] < scores[1]) return `At match ${teams[0]} - ${teams[1]}, ${teams[1]} won!`;
if(scores[0] = scores[1]) return `At match ${teams[0]} - ${teams[1]}, teams played draw.`;
}
[7 kyu] Char Code Calculation
Given a string, turn each character into its ASCII character code and join them together to create a number - let's call this number total1:
'ABC' --> 'A' = 65, 'B' = 66, 'C' = 67 --> 656667
Then replace any incidence of the number 7 with the number 1, and call this number 'total2':
total1 = 656667
total2 = 656661
Then return the difference between the sum of the digits in total1 and total2:
(6 + 5 + 6 + 6 + 6 + 7)
- (6 + 5 + 6 + 6 + 6 + 1)
---------------------------------6
解:
function calc(x){
let sum = n => [...n].reduce((a,b) => +a + +b);
let total1 = x.replace(/./g, x => x.charCodeAt(0));
let total2 = total1.replace(/7/g,'1');
return sum(total1) - sum(total2);
}
[7 kyu] Automorphic Number (Special Numbers Series #6)
Definition
A number is called Automorphic number if and only if its square ends in the same digits as the number itself.
Task
Given a number determine if it Automorphic or not .
翻译:
一个数被称为自守数,当且仅当它的平方以与数本身相同的数字结尾。
任务
给定一个数字,确定它是否自守。
解:
function automorphic(n){
return String(n*n).endsWith(String(n)) ? "Automorphic" : "Not!!"
}
[8 kyu] The 'if' function
Create a function called _if which takes 3 arguments: a boolean value bool and 2 functions (which do not take any parameters): func1 and func2
When bool is truth-ish, func1 should be called, otherwise call the func2.
Example:
_if ( true,
function() { console.log("True") },
function() { console.log("false") }
)
// Logs 'True' to the console.
翻译:
创建一个名为_if的函数,该函数接受3个参数:布尔值bool和2个函数(不接受任何参数):func1和func2
当bool为true-ish时,应该调用func1,否则调用func2。
解:
function _if(bool, func1, func2) {
return bool ? func1() : func2();
}
[7 kyu] Round up to the next multiple of 5
Given an integer as input, can you round it to the next (meaning, "greater than or equal") multiple of 5?
Examples:
input: output:
0 -> 0
2 -> 5
3 -> 5
12 -> 15
21 -> 25
30 -> 30
-2 -> 0
-5 -> -5
etc.
翻译:
四舍五入到下一个5的倍数
给定一个整数作为输入,你能把它四舍五入到下一个(意思是“大于或等于”)5的倍数吗?
解:
function roundToNext5(n){
return Math.ceil(n/5)*5;
}
[8 kyu] Exclamation marks series #1: Remove an exclamation mark from the end of string
Remove an exclamation mark from the end of a string. For a beginner kata, you can assume that the input data is always a string, no need to verify it.
Examples
remove("Hi!") == "Hi"
remove("Hi!!!") == "Hi!!"
remove("!Hi") == "!Hi"
remove("!Hi!") == "!Hi"
remove("Hi! Hi!") == "Hi! Hi"
remove("Hi") == "Hi"
翻译:
删除字符串末尾的感叹号。对于初学者kata,您可以假设输入数据始终是字符串,无需验证。
解:
function remove(s) {
return s.endsWith('!') ? s.slice(0, -1) : s;
}