亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Node.js 中比較兩個文件的好方法是什么

在 Node.js 中比較兩個文件的好方法是什么

守候你守候我 2021-12-23 15:48:08
我正在嘗試構建一個簡單的自動分級器來檢查兩個 javascript 匹配文件。這是一個可能的練習示例:var http = require('http');http.createServer((req, res) => {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.end('Hello World!');}).listen(8000,  () => {console.log('Node server is running..');});這是上述練習的解決方案:var http = require('http');var dt = require('./myfirstmodule');http.createServer((req, res) => {  res.writeHead(200, {'Content-Type': 'text/plain'});  res.write("The date and time are currently: " + dt.myDateTime());  res.end('Hello World!');}).listen(8000,  () => {console.log('Node server is running..');});你知道在 Node 中檢查練習文件是否與解決方案匹配的好方法嗎?完全匹配文件可能不是一個好主意,因為它們之間可能存在空格并且仍然是一個有效的解決方案。提前致謝。
查看完整描述

1 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

您可以使用讀取每個文件的內容,fs.readFileSync()然后使用包字符串相似度來比較它們。


安裝字符串相似度:


npm install string-similarity --save

然后在你的代碼中:


const fs = require('fs');

const stringSimilarity = require('string-similarity');


let exercise = fs.readFileSync('path/to/file/exercise.js');

let solution = fs.readFileSync('path/to/file/solution.js');


var similarity = stringSimilarity.compareTwoStrings(exercise, solution);

console.log(similarity); //Returns a fraction between 0 and 1


查看完整回答
反對 回復 2021-12-23
  • 1 回答
  • 0 關注
  • 778 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號