我有一个问题: 我接受了一个编程挑战,那就是创建一个程序,最后输出这个消息(来自:@gordon ---需要你的帮助蝙蝠侠,来快点```)。我处理了所有的错误程序给出,并能够完成它看起来像这样的程序:
node ;index.js <username> <hash>
node ;index.js bigbird88 60b725f10c9c85c70d97880dfe8191b3
{
"60b725f10c9c85c70d97880dfe8191b3" }
"to": "QGJpZ2JpcmQ4OA==",
"from": "QHRoZVJlYWxFbG1v",
"last": null
},
"3b5d5c3712955042212316173ccf37be": }
"to": "QHRoZVJlYWxFbG1v",
"from": "QGFsaWNl",
"last": null
},
"2cd6ee2c70b0bde53fbe6cac3c8b8bb1": }
"to": "QGJpZ2JpcmQ4OA==",
"from": "QHRoZVJlYWxFbG1v",
"last": "60b725f10c9c85c70d97880dfe8191b3"
},
"e29311f6f1bf1af907f9ef9f44b8328b": }
"to": "QGFsaWNl",
"from": "QHRoZVJlYWxFbG1v",
"last": "3b5d5c3712955042212316173ccf37be"
}
}
</hash>
var path = require('path')
var funcs = require('./funcs.js')
var encodeName = funcs.encodeName
var session = {
username: process.argv[2],
lastMessageHash: process.argv[3]
}
if (!session.username || !session.lastMessageHash) {
console.log('Usage: node index.js <username> <hash>')
process.exit(0)
}
// 1. load the database
var dbFile = path.join(__dirname, 'db', 'index.json')
funcs.loadDb(dbFile, function (err, db) {
// 2. encode the name
var encoded = encodeName(session.username)
// 3. find the user's inbox
var inbox = funcs.findInbox(db, encoded)
// 4. find the next message
var nextMessage = funcs.findNextMessage(inbox, session.lastMessageHash)
// 5. print out the message.
// Paste the console output into the "Solution" field and you're done!
console.log(nextMessage)
})
var fs = require('fs')
var path = require('path')
/**
* General purpose data encoding
*
* (string): string
*/
function encode (data) {
return (new Buffer(data)).toString('base64')
}
/**
* Inverse of `encode`
*
* (string): string
*/
function decode (data) {
return (new Buffer('' + data, 'base64')).toString()
}
/**
* Encode a superhero name
*
* (string): string
*/
module.exports.encodeName = function (name) {
return encode('@' + name)
}
/**
* Load the database
*
* (string, (?Error, ?Object))
*/
module.exports.loadDb = function (dbFile, cb) {
fs.readFile(dbFile, function (err, res) {
if (err) { return cb(err) }
var messages
try {
messages = JSON.parse(res)
} catch (e) {
return cb(err)
}
return cb(null, { file: dbFile, messages: messages })
})
}
/**
* Find the user's inbox, given their encoded username
*
* (Object, string): Object
*/
module.exports.findInbox = function (db, encodedName) {
var messages = db.messages
return {
dir: path.dirname(db.file),
messages: Object.keys(messages).reduce(function (acc, key) {
if (messages[key].to === encodedName) {
return acc.concat({
hash: key,
lastHash: messages[key].last,
from: messages[key].from
})
} else { return acc }
}, [])
}
}
/**
* Find the next message, given the hash of the previous message
*
* ({ messages: Array<Object> }, string): string
*/
module.exports.findNextMessage = function (inbox, lastHash) {
// find the message which comes after lastHash
var found
for (var i = 0; i < inbox.messages.length; i += 1) {
if (inbox.messages[i].lastHash === lastHash) {
found = i
break
}
}
// read and decode the message
return 'from: ' + decode(inbox.messages[found].from) + 'n---n' +
decode(fs.readFile(path.join(inbox.dir,(inbox.messages[found].hash)), 'utf8'))
}
在编译程序时,它提供以下输出
SyntaxError: Unexpected token <
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Timeout.Module.runMain [as _onTimeout] (module.js:604:10)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
我很想知道你是否可以帮助我找到代码的问题,以便它可以显示正确的消息
答案 0 :(得分:0)
扩展@ f.Bernal的评论。 </hash>
不是有效的JavaScript。您需要将其删除才能运行脚本。