Files
30-seconds-of-code/snippets/yesNo.md
Angelos Chalaris 611729214a Snippet format update
To match the starter (for the migration)
2019-08-13 10:29:12 +03:00

21 lines
499 B
Markdown

---
title: yesNo
tags: utility,regexp,intermediate
---
Returns `true` if the string is `y`/`yes` or `false` if the string is `n`/`no`.
Use `RegExp.test()` to check if the string evaluates to `y/yes` or `n/no`.
Omit the second argument, `def` to set the default answer as `no`.
```js
const yesNo = (val, def = false) =>
/^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
```
```js
yesNo('Y'); // true
yesNo('yes'); // true
yesNo('No'); // false
yesNo('Foo', true); // true
```