Sunday, 19 March 2017

Functions doesn't work - NodeJs -

I have an issue , my functions don't work , I don't know why ?

my code is an interpreter with Nodejs to control a ROBOT to move forward , backward & jump .

This is my code :

/*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;
}

The robot.txt contains :

# Move
FORWARD 200
COMMAND_WITH_ERROR 
BACKWARD 10

and it should print :

OK 200, 0, 90
ERROR LINE 2: Unknown command COMMAND_WITH_ERROR
OK 190, 0, 90

but my program prints only : x= 0 , y = 0 , r = 90

Anything wrong ?

PS : I used jshint and i got this error :

intersting.js: line 39, col 33, 'c' used out of scope.



via infocrasher

No comments:

Post a Comment