首页 > 其他 > 详细

Responsible Drinking

时间:2020-04-08 12:59:12      阅读:73      评论:0      收藏:0      [点我收藏+]

Welcome to the Codewars Bar!

Codewars Bar recommends you drink 1 glass of water per standard drink so you‘re not hungover tomorrow morning.

Your fellow coders have bought you several drinks tonight in the form of a string. Return a string suggesting how many glasses of water you should drink to not be hungover.

Example parties:

Input 0:

"1 beer"

Output 0:

"1 glass of water"

Explaination 0:

You drank one standard drink

Input 1:

题目:

"1 shot, 5 beers, 2 shots, 1 glass of wine, 1 beer"

Output 1:

"10 glasses of water"

Explaination 1:

You drank ten standard drinks

Note:

To keep the things simple, we‘ll considere that any "numbered thing" in the string is a drink. Even "1 bear" => "1 glass of water" or "1 chainsaw and 2 pools" => "3 glasses of water"...


FUNDAMENTALS
LOOPS
CONTROL FLOW
 
答案:

function hydrate(s) {
if(!s){
return ‘1 beer‘
}
let sum=0,f=‘ glasses of water‘;
let str = s.replace(/\d+/g,function(){
sum += parseInt(arguments[0]);
});
if(sum==1||sum==0){
f=‘ glass of water‘
}
return sum+f
}

BASIC LANGUAGE FEATURES

Responsible Drinking

原文:https://www.cnblogs.com/yinghuang/p/12658940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!