[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 .
翻譯:
一個(gè)數(shù)被稱為自守?cái)?shù),當(dāng)且僅當(dāng)它的平方以與數(shù)本身相同的數(shù)字結(jié)尾。
任務(wù)
給定一個(gè)數(shù)字,確定它是否自守。
解:
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.
翻譯:
創(chuàng)建一個(gè)名為_if的函數(shù),該函數(shù)接受3個(gè)參數(shù):布爾值bool和2個(gè)函數(shù)(不接受任何參數(shù)):func1和func2
當(dāng)bool為true-ish時(shí),應(yīng)該調(diào)用func1,否則調(diào)用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.
翻譯:
四舍五入到下一個(gè)5的倍數(shù)
給定一個(gè)整數(shù)作為輸入,你能把它四舍五入到下一個(gè)(意思是“大于或等于”)5的倍數(shù)嗎?
解:
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"
翻譯:
刪除字符串末尾的感嘆號(hào)。對(duì)于初學(xué)者kata,您可以假設(shè)輸入數(shù)據(jù)始終是字符串,無需驗(yàn)證。
解:
function remove(s) {
return s.endsWith('!') ? s.slice(0, -1) : s;
}