2017-06-12 55 views
-3

我有一個問題: 我被給了一個編程挑戰,是創建一個程序,在最後輸出這個消息(from:@gordon --- need your help batman,come quick```)。我的工作在所有的都在給定的程序中的錯誤,並能完成它看起來像這樣的程序:Javascritpt程序來顯示某個消息

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) 

我在問你是否可以幫我找到問題與代碼,以便它可以顯示正確的消息

+1

轉到文件vm.js中的第56行,有一個

+0

感謝您的幫助man.let我試試看。 – Adrizy

回答

0

展開@ f.Bernal的評論。 </hash>是無效的JavaScript。您需要將其刪除才能運行腳本。