Skip to content

Instantly share code, notes, and snippets.

@peterlikesnoodles
peterlikesnoodles / eva.js
Created September 14, 2020 05:13
Evaluate expression with -, + and *.
const eva = (str) => add(str)
const add = str => str.split('+').reduce((total, exp) => {
const s = exp.trim()
const num = Number(s)
return total + (isNaN(num) ? subtract(s) : num)
}, 0)
const subtract = str => str.split('-').reduce((total, exp, index) => {
const s = exp.trim()
/**
* str 结构是 `${str1} ${str2}`.
* 二个字符串用空格分开。
*
* str1 描述以下3种规则:
* - + 匹配单一写了字符
* - * 匹配3个连续的相同字符
* - *{N} 匹配 N 个连续的相同字符(比如 *{12} 匹配12个相同字符)
* @returns str2 是否完全符合 str1 的格式表示
*/

Keybase proof

I hereby claim:

  • I am peterlikesnoodles on github.
  • I am wanglu (https://keybase.io/wanglu) on keybase.
  • I have a public key ASB_igEyJpnVR-5Jke6V9Rd-gKMsgKU-lq6oeZ10R4grhAo

To claim this, I am signing this object: