题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); let dict = [ { fir: 'reset', sec: ' ', val: 'reset what' }, { fir: 'reset', sec: 'board', val: 'board fault' }, { fir: 'board', sec: 'add', val: 'where to add' }, { fir: 'board', sec: 'delete', val: 'no board at all' }, { fir: 'reboot', sec: 'backplane', val: 'impossible' }, { fir: 'backplane', sec: 'abort', val: 'install first' } ] rl.on('line', function (line) { handler(line.trim()) }); function handler (s) { let [part1, part2] = s.split(' ') let res = dict.filter(item => { return item.fir.startsWith(part1) && (part2 ? item.sec.startsWith(part2): item.sec == ' ') }) if (res.length == 1) return console.log(res[0].val) console.log('unknown command') }