codewars(2)

402 阅读1分钟

1.ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.If the function is passed a valid PIN string, return true, else return false.(匹配4或6个数字)

/^(\d{4}){1}(\d{2})?&/.test(str)
/* ? 匹配零个或一个。正则表达式本身为贪婪匹配尽可能多的匹配,加上'?'为非贪婪匹配尽可能少的匹配

2.Given an array of ones and zeroes, convert the equivalent binary value to an integer.
Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1.

function fun(arr) {
    return parseInt(arr.join(''), 2)
}
/**
parseInt(string, radix)
string: 必选。要转化的字符串
redix:可选。表示要解析的数字的基数*/