写出一个程序,接受一个十六进制的数,输出该数值的十进制表示
语言:Js
代码:
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here while ((line = await readline())) { let str = line.substring(2).toUpperCase(); let tokens = str.split(''); let obj = { A: "10", B: "11", C: "12", D: "13", E: "14", F: "15", a: "10", b: "11", c: "12", d: "13", e: "14", f: "15", }; let resultArr = []; tokens.forEach((item) => { Object.keys(obj).forEach((key) => { if (item === key) { resultArr.push(obj[key]); } }); }); let sum = 0; resultArr.forEach((val, index) => { let newVal = val * Math.pow(16, index); sum += newVal; }); console.log(sum); } })();