我有问题,我的功能不起作用,我不知道为什么?
我的代码是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{
command = fs.readFileSync(filename).toString().split('\r\n');
}
catch(e){
console.error("I can't read from the file " +e);
}
var line = [];
robotInterpreter(command);
console.log('OK' , ' ' + x + ',' + y + ',' + r);
function robotInterpreter (value){
for (var i in value) {
var okC =0 ;
var a = value[i].trim();
line.push(a.split(' '));
for(var j in line){
if(okC === 0)
verificationOfCommand(line[i][j]);
}
if (okC === 1){
if (rep === 0){
var c = a.substring(n).trim().split(',');
if(f === 1) verificationOfF(c) ;
if(b === 1) VerificationOfB(c) ;
if(t === 1) verificationOfT(c) ;
if(j === 1) verficationOfJ(c) ;
}else{
verificationOfR(c);
}
}
}
}
function verificationOfE(value){
if(value.length > 1){
console.log('ERROR LINE' + i + ':END has 0 parameters, you wrote' + value.length);
}
}
function verificationOfR(value){
if (value.length > 1) {
console.log('ERROR LINE' + i + 'REPEAT has 1 parameters , you wrote' +value.length);
}else
if(isNaN(value[0])){
console.log('ERROR LINE' + i + 'REPEAT parameter 1 requires a number , you wrote' +value[0]);
//}else
//if(verificationOfR())
}
}
function verificationOfComment(value){
var a = value.indexOf('#');
if (a !== (-1)){
console.log(value);
}
}
function verficationOfJ(value){
console.log('YES');
if (value.length > 2 ){
console.log('ERROR LINE' + i + ':JUMP has 2 parameters , you wrote '+value.length);
}else
if(isNaN(value[0])){
console.log('ERROR LINE' + i + ':JUMP parameter 1 requires a number , you wrote '+value[0]);
}else
if(isNaN(value[1])){
console.log('ERROR LINE' +i+ ':JUMP parameter 2 requires a number , you wrote' +value[1]);
}else {
jump(value[0],value[1]);
j= 0;
}
}
function verificationOfF(value){
if (value.length > 1){
console.log('ERROR LINE' +i+ ':FORWARD has 1 parameter , you wrote ' + value.length);
}else
if (isNaN(value[0])){
console.log('ERROR LINE' +i+ ':FORWARD parameter 1 requires a number , you wrote '+ value[0]);
}else{
forward(value[0]);
f = 0 ;
}
}
function verificationOfT(value){
if (value[0].trim() === "left"){
le = 1 ;
}
if(value[0].trim() === "right"){
ri= 1;
}
if (value.length > 2){
console.log('ERROR LINE' +i+ ':TURN has 2 parameters , you wrote '+value.length);
}else
if(isNaN(value[1])){
console.log('ERROR LINE' +i+ ':TURN parameter 2 requires a number , you wrote'+value[1]);
}else
if (le === 0 && ri === 0) {
console.log('ERROR LINE' +i+ ':TURN parameter requires a left/right , you wrote'+value[0]);
}else{
if (le === 1) {
turnL(value[1]);
t = 0 ;
le = 0 ;
}
if (ri === 1) {
turnR(value[1]);
t = 0 ;
ri= 0 ;
}
}
}
function VerificationOfB(value){
if(value.length > 1){
console.log('ERROR LINE'+i+ ':BACKWARD has 1 parameter , you wrote' + value.length);
}else
if(isNaN(value[0])){
console.log('ERROR LINE' +i+ ':BACKWARD parameter 1 requires a number , you wrote'+value[0]);
}else{
backward(value[0]);
b = 0;
}
}
function verificationOfCommand(value){
if(value === "FORWARD"){
f = 1 ;
okC = 1 ;
n = value.length;
}else
if(value === "BACKWARD"){
b= 1 ;
okC = 1 ;
n = value.length;
}else
if (value === "JUMP"){
j = 1 ;
okC = 1 ;
n = value.length;
}else
if(value === "TURN"){
t = 1 ;
okC = 1 ;
n = value.length ;
}else
if (value === "REPEAT"){
lr = i ;
rep = 1 ;
n = value.length;
}else
if(value === "END"){
end = 1 ;
n = value.length;
}
}
function forward (value){
x = x + parseInt(value);
}
function backward (value){
x = x - parseInt(value);
}
function turnL(value){
var k = parseInt(value);
while (k >= 360){
k= k - 360 ;
}
if (r + k > 360)
r = (r+ k) - 360 ;
else
r = r+ k ;
if(r === 360 )
r= 0 ;
}
function turnR(value){
var k = parseInt(value);
while(k >= 360){
k = k - 360 ;
}
if (r - k < 0 )
r = 360 - (k-r);
else
r = r - k ;
if (r === 360)
r = 0 ;
}
function jump(var1, var2){
x = parseInt(var1);
y = parseInt(var2);
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.
答案 0 :(得分:0)
if (rep === 0){
var c = a.substring(n).trim().split(',');
...
// c is defined here!
verificationOfR(c);
} else {
// c is not defined here!
verificationOfR(c);
}
在上面的代码中,你没有在else语句中定义c。在if else语句中,它将采用一个或另一个路径,而不是两个路径。
您需要执行以下操作
if (rep === 0){
var c = a.substring(n).trim().split(',');
...
verificationOfR(c);
} else {
var c = a.substring(n).trim().split(',');
// c is *now* defined here!
verificationOfR(c);
}