0
我有一個問題,我的函數不工作,我不知道爲什麼?函數不起作用 - NodeJs -
我的代碼是一個帶Nodejs的解釋器來控制一個ROBOT向前移動,向後移動&跳轉。
這是我的代碼:
/*jshint node: true */
"use strict";
var fs = require('fs');
var filename = "./robot.txt";
var command = null ;
var x= 0 , y = 0 , r = 90 , j = 0 , n= 0 , le= 0 , ri= 0 , rep = 0 , end = 0, lr= 0 , f = 0 , i= 0 , okC = 0 , b = 0 , t= 0 ;
try{
\t command = fs.readFileSync(filename).toString().split('\r\n');
}
catch(e){
\t console.error("I can't read from the file " +e);
}
var line = [];
robotInterpreter(command);
console.log('OK' , ' ' + x + ',' + y + ',' + r);
function robotInterpreter (value){
\t for (var i in value) {
\t \t var okC =0 ;
\t \t var a = value[i].trim();
\t \t line.push(a.split(' '));
\t \t for(var j in line){
\t \t \t if(okC === 0)
\t \t \t \t verificationOfCommand(line[i][j]);
\t \t }
\t \t if (okC === 1){
\t \t \t if (rep === 0){
\t \t \t \t var c = a.substring(n).trim().split(',');
\t \t \t \t if(f === 1) verificationOfF(c) ;
\t \t \t \t if(b === 1) VerificationOfB(c) ;
\t \t \t if(t === 1) verificationOfT(c) ;
\t \t \t if(j === 1) verficationOfJ(c) ;
\t \t \t }else{
\t \t \t \t verificationOfR(c);
\t \t \t }
\t \t \t
\t \t }
\t \t
\t }
}
function verificationOfE(value){
\t if(value.length > 1){
\t \t console.log('ERROR LINE' + i + ':END has 0 parameters, you wrote' + value.length);
\t }
}
function verificationOfR(value){
\t if (value.length > 1) {
\t \t console.log('ERROR LINE' + i + 'REPEAT has 1 parameters , you wrote' +value.length);
\t }else
\t if(isNaN(value[0])){
\t \t console.log('ERROR LINE' + i + 'REPEAT parameter 1 requires a number , you wrote' +value[0]);
\t //}else
\t //if(verificationOfR())
}
}
function verificationOfComment(value){
\t var a = value.indexOf('#');
\t if (a !== (-1)){
\t \t console.log(value);
\t }
}
function verficationOfJ(value){
\t console.log('YES');
\t if (value.length > 2){
\t \t console.log('ERROR LINE' + i + ':JUMP has 2 parameters , you wrote '+value.length); \t
\t }else
\t if(isNaN(value[0])){
\t \t console.log('ERROR LINE' + i + ':JUMP parameter 1 requires a number , you wrote '+value[0]);
\t }else
\t if(isNaN(value[1])){
\t \t console.log('ERROR LINE' +i+ ':JUMP parameter 2 requires a number , you wrote' +value[1]);
\t }else {
\t \t jump(value[0],value[1]);
\t \t j= 0;
\t }
}
function verificationOfF(value){
\t if (value.length > 1){
\t \t console.log('ERROR LINE' +i+ ':FORWARD has 1 parameter , you wrote ' + value.length);
\t }else
\t if (isNaN(value[0])){
\t \t console.log('ERROR LINE' +i+ ':FORWARD parameter 1 requires a number , you wrote '+ value[0]);
\t }else{
\t \t forward(value[0]);
\t \t f = 0 ;
\t }
}
function verificationOfT(value){
\t if (value[0].trim() === "left"){
\t \t le = 1 ;
\t }
\t if(value[0].trim() === "right"){
\t \t ri= 1;
\t }
\t if (value.length > 2){
\t \t console.log('ERROR LINE' +i+ ':TURN has 2 parameters , you wrote '+value.length);
\t }else
\t if(isNaN(value[1])){
\t \t console.log('ERROR LINE' +i+ ':TURN parameter 2 requires a number , you wrote'+value[1]);
\t }else
\t if (le === 0 && ri === 0) {
\t \t console.log('ERROR LINE' +i+ ':TURN parameter requires a left/right , you wrote'+value[0]);
\t }else{
\t \t if (le === 1) {
\t \t \t turnL(value[1]);
\t \t \t t = 0 ;
\t \t \t le = 0 ;
\t \t }
\t \t if (ri === 1) {
\t \t \t turnR(value[1]);
\t \t \t t = 0 ;
\t \t \t ri= 0 ;
\t \t }
\t }
}
function VerificationOfB(value){
\t if(value.length > 1){
\t \t console.log('ERROR LINE'+i+ ':BACKWARD has 1 parameter , you wrote' + value.length);
\t }else
\t if(isNaN(value[0])){
\t \t console.log('ERROR LINE' +i+ ':BACKWARD parameter 1 requires a number , you wrote'+value[0]);
\t }else{
\t \t backward(value[0]);
\t \t b = 0;
\t }
}
function verificationOfCommand(value){
\t if(value === "FORWARD"){
\t \t f = 1 ;
\t \t okC = 1 ;
\t \t n = value.length;
\t }else
\t if(value === "BACKWARD"){
\t \t b= 1 ;
\t \t okC = 1 ;
\t \t n = value.length;
\t }else
\t if (value === "JUMP"){
\t \t j = 1 ;
\t \t okC = 1 ;
\t \t n = value.length;
\t }else
\t if(value === "TURN"){
\t \t t = 1 ;
\t \t okC = 1 ;
\t \t n = value.length ;
\t }else
\t if (value === "REPEAT"){
\t \t lr = i ;
\t \t rep = 1 ;
\t \t n = value.length;
\t }else
\t if(value === "END"){
\t \t end = 1 ;
\t \t n = value.length;
\t }
}
function forward (value){
\t x = x + parseInt(value);
}
function backward (value){
\t x = x - parseInt(value);
}
function turnL(value){
\t var k = parseInt(value);
\t while (k >= 360){
\t \t k= k - 360 ;
\t }
\t if (r + k > 360)
\t \t r = (r+ k) - 360 ;
\t else
\t \t r = r+ k ;
\t if(r === 360)
\t \t r= 0 ;
}
function turnR(value){
\t var k = parseInt(value);
\t while(k >= 360){
\t \t k = k - 360 ;
\t }
\t if (r - k < 0)
\t \t r = 360 - (k-r);
\t else
\t \t r = r - k ;
\t if (r === 360)
\t \t r = 0 ;
}
function jump(var1, var2){
\t x = parseInt(var1);
\t y = parseInt(var2);
\t j = 0;
}
的robot.txt的包含:
# Move
FORWARD 200
COMMAND_WITH_ERROR
BACKWARD 10
,它應該打印:
OK 200, 0, 90
ERROR LINE 2: Unknown command COMMAND_WITH_ERROR
OK 190, 0, 90
但我的程序僅打印:x = 0,y = 0,r = 90
有什麼問題嗎?
PS:我用jshint,我得到這個錯誤:
intersting.js: line 39, col 33, 'c' used out of scope.
嗯,棉短絨警告當然是正確的,你初始化變量在當時的分支,並試圖在else分支使用它。 – Bergi
非常感謝您的回覆,但我對您的理解並不理想,請您向我澄清一些事情? – infocrasher