codewars练习记录17 js

[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 !!" 

}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,445评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,889评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,047评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,760评论 1 276
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,745评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,638评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,011评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,669评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,923评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,655评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,740评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,406评论 4 320
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,995评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,961评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,023评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,483评论 2 342

推荐阅读更多精彩内容