本文主要是介绍0059【Edabit ★☆☆☆☆☆】【是否被允许喝酒】Drinks Allowed?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
0059【Edabit ★☆☆☆☆☆】【是否被允许喝酒】Drinks Allowed?
conditions
language_fundamentals
logic
validation
Instructions
A bartender is writing a simple program to determine whether he should serve drinks to someone. He only serves drinks to people 18 and older and when he’s not on break.
Given the person’s age, and whether break time is in session, create a function which returns whether he should serve drinks.
Examples
shouldServeDrinks(17, true) // false
shouldServeDrinks(19, false) // true
shouldServeDrinks(30, true) // false
Notes
- Return
true
orfalse
. - Some countries have a slightly higher drinking age, but for the purposes of this challenge, it will be 18.
Solutions
function shouldServeDrinks(age, onBreak) {return age>=18 && !onBreak
}
TestCases
let Test = (function(){return {assertEquals:function(actual,expected){if(actual !== expected){let errorMsg = `actual is ${actual},${expected} is expected`;throw new Error(errorMsg);}},assertSimilar:function(actual,expected){if(actual.length != expected.length){throw new Error(`length is not equals, ${actual},${expected}`);}for(let a of actual){if(!expected.includes(a)){throw new Error(`missing ${a}`);}}}}
})();Test.assertEquals(shouldServeDrinks(17, true), false)
Test.assertEquals(shouldServeDrinks(30, true), false)
Test.assertEquals(shouldServeDrinks(24, false), true)
Test.assertEquals(shouldServeDrinks(18, false), true)
Test.assertEquals(shouldServeDrinks(3, false), false)
这篇关于0059【Edabit ★☆☆☆☆☆】【是否被允许喝酒】Drinks Allowed?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!