【8 kyu】Sum of Multiples
Your Job
Find the sum of all multiples of n below m
Keep in Mind
n and m are natural numbers (positive integers)
m is excluded from the multiples
Examples
sumMul(2, 9) ==> 2 + 4 + 6 + 8 = 20
sumMul(3, 13) ==> 3 + 6 + 9 + 12 = 30
sumMul(4, 123) ==> 4 + 8 + 12 + ... = 1860
sumMul(4, -7) ==> "INVALID"
翻译:
你的工作
求m以下n的所有倍数之和
牢记
n和m是自然数(正整数)
m从倍数中排除
解:
function sumMul(n,m){
let sum=0
for(let i=0;i<m;i+=n){
sum=sum+i
}
return n<m ? sum : "INVALID"
}
【8 kyu】Find Multiples of a Number
In this simple exercise, you will build a program that takes a value, integer , and returns a list of its multiples up to another value, limit . If limit is a multiple of integer, it should be included as well. There will only ever be positive integers passed into the function, not consisting of 0. The limit will always be higher than the base.
For example, if the parameters passed are (2, 6), the function should return [2, 4, 6] as 2, 4, and 6 are the multiples of 2 up to 6.
If you can, try writing it in only one line of code.
翻译:
在这个简单的练习中,您将构建一个程序,该程序接受一个值integer,并返回其倍数列表,直到另一个值limit。如果limit是整数的倍数,则也应包括它。只会有正整数传递到函数中,而不是由0组成。限制将始终高于基数。
例如,如果传递的参数是(2,6),函数应该返回[2,4,6],因为2,4和6是2到6的倍数。
如果可以的话,试着只用一行代码编写它。
解一:
function findMultiples(integer, limit){
let result = []
for (let i = integer; i<=limit; i+=integer)
result.push(i)
return result
}
解二:
function findMultiples(integer,limit){
return Array(Math.floor(limit/integer)).fill(1).map((x,i)=>integer*(i+1));
}
【7 kyu】Speed Control
In John's car the GPS records every s seconds the distance travelled from an origin (distances are measured in an arbitrary but consistent unit). For example, below is part of a record with s = 15:
x = [0.0, 0.19, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25]
The sections are:
0.0-0.19, 0.19-0.5, 0.5-0.75, 0.75-1.0, 1.0-1.25, 1.25-1.50, 1.5-1.75, 1.75-2.0, 2.0-2.25
We can calculate John's average hourly speed on every section and we get:
[45.6, 74.4, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]
Given s and x the task is to return as an integer the floor of the maximum average speed per hour obtained on the sections of x. If x length is less than or equal to 1 return 0 since the car didn't move.
Example:
with the above data your function gps(s, x)should return 74
Note
With floats it can happen that results depends on the operations order. To calculate hourly speed you can use:
翻译:
在约翰的汽车中,GPS每秒钟记录一次从原点行驶的距离(距离是以任意但一致的单位测量的)。例如,下面是s=15的记录的一部分:
x=[0.0、0.19、0.5、0.75、1.0、1.25、1.5、1.75、2.0、2.25]
这些部分包括:
0.0-0.19, 0.19-0.5, 0.5-0.75, 0.75-1.0, 1.0-1.25, 1.25-1.50, 1.5-1.75, 1.75-2.0, 2.0-2.25
我们可以计算每个路段上约翰的平均每小时速度,我们得到:
[45.6, 74.4, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0]
给定s和x,任务是以整数形式返回x段上获得的每小时最大平均速度的floor。如果x长度小于或等于1,则返回0,因为汽车没有移动。
例子:
使用上面的数据,函数gps(s,x)应该返回74
笔记
对于浮动,结果可能取决于操作顺序。要计算每小时速度,可以使用:
(3600*增量距离)/s。
解一:
function gps(s, x) {
if (x.length<=1) {
return 0;
}
let arr=[]
for (let i = 0; i < x.length - 1; i++) {
arr.push(x[i + 1] - x[i])
}
return Math.max(...arr.map(x => Math.floor((3600 * x) / s)))
}
解二:
function gps(s, x) {
return Math.max(...x.slice(1).map((a, i) => (a - x[i]) / s * 3600)) | 0
}
【8 kyu】Is there a vowel in there?
Given an array of numbers, check if any of the numbers are the character codes for lower case vowels (a, e, i, o, u).
If they are, change the array value to a string of that vowel.
Return the resulting array.
翻译:
给定一组数字,检查是否有任何数字是小写元音(a,e,i,o,u)的字符代码。
如果是,请将数组值更改为该元音的字符串。
返回结果数组。
解一:
function isVow(a){
return a.map(x => /[aeiou]/.test(String.fromCharCode(x)) ? String.fromCharCode(x) : x);
}
解二:
function isVow(a){
for (var i=0, l=a.length; i<l; ++i) {
var char = String.fromCharCode(a[i])
if ('aeiou'.indexOf(char) !== -1)
a[i] = char;
}
return a;
}
【8 kyu】Holiday VIII - Duty Free
The purpose of this kata is to work out just how many bottles of duty free whiskey you would have to buy such that the saving over the normal high street price would effectively cover the cost of your holiday.
You will be given the high street price (normPrice), the duty free discount (discount) and the cost of the holiday.
For example, if a bottle cost £10 normally and the discount in duty free was 10%, you would save £1 per bottle. If your holiday cost £500, the answer you should return would be 500.
All inputs will be integers. Please return an integer. Round down.
翻译:
这个kata的目的是计算出你要买多少瓶免税威士忌,这样比正常的商业街价格节省下来的钱就能有效地支付你度假的费用。
您将获得商业街价格(normalPrice)、免税折扣(discount)和度假费用。
例如,如果一瓶酒的正常价格为10英镑,免税折扣为10%,那么每瓶可以节省1英镑。如果你的假期花费500英镑,你应该返回500英镑。
所有输入都是整数。请返回一个整数。四舍五入。
解:
function dutyFree(normPrice, discount, hol){
return(Math.floor(hol / normPrice / discount * 100))
}