Edabit ★☆☆☆☆☆】【是否被允许喝酒】Drinks Allowed?"/>
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?
发布评论