文章目录
[8 kyu] Find Nearest square number
Your task is to find the nearest square number, nearest_sq(n), of a positive integer n.
翻译:
您的任务是找到正整数n的最近平方数nearest_sq(n)。
解:
function nearestSq(n){
return Math.pow(Math.round(Math.sqrt(n)), 2);
}
[8 kyu] Plural
We need a simple function that determines if a plural is needed or not. It should take a number, and return true if a plural should be used with that number or false if not. This would be useful when printing out a string such as 5 minutes, 14 apples, or 1 sun.
You only need to worry about english grammar rules for this kata, where anything that isn’t singular (one of something), it is plural (not one of something).
All values will be positive integers or floats, or zero.
翻译:
我们需要一个简单的函数来确定是否需要复数。它应该接受一个数字,如果复数应该与该数字一起使用,则返回true,如果不应该,则返回false。当打印出5分钟、14个苹果或1个太阳等字符串时,这将非常有用。
你只需要担心这个kata的英语语法规则,任何不是单数的东西(某个事物之一)都是复数的(不是某个事物的一个)。
所有值都将是正整数或浮点数或零。
解:
function plural(n) {
return n !== 1;
}
[8 kyu] Simple validation of a username with regex
Write a simple regex to validate a username. Allowed characters are:
lowercase letters,
numbers,
underscore
Length should be between 4 and 16 characters (both included).
解:
function validateUsr(username) {
return /^[0-9a-z_]{4,16}$/.test(username)
}
[7 kyu] max diff - easy
You must implement a function that returns the difference between the largest and the smallest value in a given list / array (lst) received as the parameter.
lst contains integers, that means it may contain some negative numbers
if lst is empty or contains a single element, return 0
lst is not sorted
[1, 2, 3, 4] // returns 3 because 4 - 1 == 3
[1, 2, 3, -4] // returns 7 because 3 - (-4) == 7
翻译:
您必须实现一个函数,该函数返回作为参数接收的给定列表/数组(lst)中最大值和最小值之间的差值。
lst包含整数,这意味着它可能包含一些负数
如果lst为空或包含单个元素,则返回0
lst未排序
解:
function maxDiff(list) {
return list.length ? Math.max(...list) - Math.min(...list) : 0;
};
[7 kyu] Build a square
I will give you an integer. Give me back a shape that is as long and wide as the integer. The integer will be a whole number between 1 and 50.
Example
n = 3, so I expect a 3x3 square back just like below as a string:
+++
+++
+++
翻译:
我给你一个整数。给我一个和整数一样长和宽的形状。整数将是介于1和50之间的整数。
解:
function generateShape(n){
return ("+".repeat(n)+"\n").repeat(n).trim()
}
解二:
function generateShape(integer){
let str = ''
for (let i = 1; i <= integer; i++) {
for (let j = 0; j < integer; j++) {
str = str + '+'
}
str = str + '\n'
}
let arr = str.split('\n')
arr.pop()
return arr.join('\n')
}
[8 kyu] Template Strings
Template Strings
Template Strings, this kata is mainly aimed at the new JS ES6 Update introducing Template Strings
Task
Your task is to return the correct string using the Template String Feature.
Input
Two Strings, no validation is needed.
Output
You must output a string containing the two strings with the word
' are '
解:
var TempleStrings = function(obj, feature) {
return `${obj} are ${feature}`;
}