[7 kyu] Deodorant Evaporator
This program tests the life of an evaporator containing a gas.
We know the content of the evaporator (content in ml), the percentage of foam or gas lost every day (evap_per_day) and the threshold (threshold) in percentage beyond which the evaporator is no longer useful. All numbers are strictly positive.
The program reports the nth day (as an integer) on which the evaporator will be out of use.
Example:
evaporator(10, 10, 5) -> 29
Note:
Content is in fact not necessary in the body of the function "evaporator", you can use it or not use it, as you wish. Some people might prefer to reason with content, some other with percentages only. It's up to you but you must keep it as a parameter because the tests have it as an argument.
翻译:
该程序测试含有气体的蒸发器的寿命。
我们知道蒸发器的含量(content in m以毫升为单位的含量)、每天损失的泡沫或气体的百分比(evap_per_day)以及蒸发器不再使用的百分比阈值(threshold)。所有数字都是正数。
程序报告蒸发器停用的第n天(整数)。
例子:
蒸发器(10、10、5)->29
注:
事实上,内容在“蒸发器”功能的主体中是不必要的,您可以根据需要使用或不使用它。有些人可能更喜欢用内容来推理,而另一些人只喜欢用百分比来推理。这取决于您,但您必须将其作为参数保存,因为测试将其作为一个参数。
解:
function evaporator(content, evap_per_day, threshold){
var days = 0;
var gas = 100;
while(gas >= threshold){
gas -= gas * evap_per_day / 100;
days++;
}
return days;
}
[8 kyu] Who ate the cookie?
For this problem you must create a program that says who ate the last cookie. If the input is a string then "Zach" ate the cookie. If the input is a float or an int then "Monica" ate the cookie. If the input is anything else "the dog" ate the cookie. The way to return the statement is: "Who ate the last cookie? It was (name)!"
Ex: Input = "hi" --> Output = "Who ate the last cookie? It was Zach! (The reason you return Zach is because the input is a string)
Note: Make sure you return the correct message with correct spaces and punctuation.
Please leave feedback for this kata. Cheers!
翻译:
对于这个问题,你必须创建一个程序,说明谁吃了最后一块饼干。如果输入是一个字符串,那么“扎克”吃了这个饼干。如果输入是浮点或整数,那么“Monica”吃了饼干。如果输入是别的什么,“狗”吃了饼干。返回声明的方式是:“谁吃了最后一块饼干?是(名字)!”
注意:确保返回的消息正确,并带有正确的空格和标点符号。
解:
function cookie(x){
let name = '';
if (typeof x == 'number') name = 'Monica'
else if (typeof x == 'string') name = 'Zach'
else name = 'the dog'
return 'Who ate the last cookie? It was ' + name + '!'
}
[8 kyu] Determine offspring sex based on genes XX and XY chromosomes
The male gametes or sperm cells in humans and other mammals are heterogametic and contain one of two types of sex chromosomes. They are either X or Y. The female gametes or eggs however, contain only the X sex chromosome and are homogametic.
The sperm cell determines the sex of an individual in this case. If a sperm cell containing an X chromosome fertilizes an egg, the resulting zygote will be XX or female. If the sperm cell contains a Y chromosome, then the resulting zygote will be XY or male.
Determine if the sex of the offspring will be male or female based on the X or Y chromosome present in the male's sperm.
If the sperm contains the X chromosome, return "Congratulations! You're going to have a daughter."; If the sperm contains the Y chromosome, return "Congratulations! You're going to have a son.";
解:
function chromosomeCheck(sperm) {
return `Congratulations! You're going to have a ${sperm === 'XY' ? 'son' : 'daughter'}.`
}
[7 kyu] Divide and Conquer
Given a mixed array of number and string representations of integers, add up the non-string integers and subtract this from the total of the string integers.
Return as a number.
翻译:
给定整数的数字和字符串表示的混合数组,将非字符串整数相加并从字符串整数的总数中减去。
作为数字返回。
解一:
function divCon(x){
return x.reduce((a, b) => typeof b === 'number'? a+ b: a - Number(b),0)
}
解二:
function divCon(x){
let a = x.filter(x => typeof (x) == 'number').reduce((a, b) => a + b, 0)
let b = x.filter(x => typeof (x) != 'number').map(Number).reduce((a, b) => a + b, 0)
return a - b
}
[8 kyu] Price of Mangoes
There's a "3 for 2" (or "2+1" if you like) offer on mangoes. For a given quantity and price (per mango), calculate the total cost of the mangoes.
Examples
mango(3, 3) ==> 6 # 2 mangoes for 3 = 6; +1 mango for free
mango(9, 5) ==> 30 # 6 mangoes for 5 = 30; +3 mangoes for free
*每三个里面就有一个免费
解:
function mango(quantity, price){
return price * (quantity - Math.floor(quantity / 3));
}
[7 kyu] Strong Number (Special Numbers Series #2)
Strong number is the number that the sum of the factorial of its digits is equal to number itself.
For example, 145 is strong, since 1! + 4! + 5! = 1 + 24 + 120 = 145.
Task
Given a number, Find if it is Strong or not and return either "STRONG!!!!" or "Not Strong !!".
Notes
Number passed is always Positive.
Return the result as String
翻译:
强数是其数字的阶乘之和等于数字本身的数。
例如,145很强,因为1!+4! + 5! = 1 + 24 + 120 = 145.
任务
给定一个数字,确定它是否强,然后返回“强!!!”或“不强!!”。
笔记
传递的数字始终为正数。
将结果返回为字符串
解:
function strong(n) {
let a=n.toString().split('').map(function (x) {
var sum = 1
for (var i = 1; i <= x; i++) {
sum *= i
}
return sum
}).map(Number).reduce((a, b) => a + b)
return a==n?"STRONG!!!!" :"Not Strong !!"
}