Function for receiving commands

Hey there,
I’m working on this project and I got stuck in this problem.

This is my code so far

function receiver() {
  for(var i = 0; i < rover.length; i++) {
   if(moveForward() === 'f') {
     console.log(moveForward());
   } else if(turnRight() === 'r') {
     console.log(turnRight());
   } else if(turnLeft() === 'l') {
     console.log(turnLeft());
   } else {
     console.log('Error Command')
   }
  }

receiver();

Any help I will be greatful for it

In your if statements, what are you exactly tring to compare with the “f”? Are you trying to compare moveForward with f?

No I’m actually trying to put it equal to it. So if I call the ‘f’ it will moveForward

this is my function for moving forward;

function moveForward() {
	console.log('moveForward was called');
  rover.travelLog.push("Moved Forward")
	switch (rover.direction) {
		case 'W':
			rover.position[0]--;
			break;
		case 'N':
			rover.position[1]--;
			break;
		case 'S':
			rover.position[1]++;
			break;
	}
}

Good to know. So in the 1st block of code you’ve given me, you’re comparing moveForward() with ‘f’. Since that’s not what you want to do, then you need to fix it to what you do want.

I’m sorry I didn’t get it, the first block of code it’s wrong what is it that I have to fix again?

These two statements comparing a function with a string with ===. Is that what you want to compare?

Actually I’m trying to set equal to, so when I call that letter for example ‘f’ I want it to moveForward();

I understand that’s what you want. But the purpose of an if statement is to let the program know whether to proceed or not, not to process a function (unless the function aids in comparing values).

Thanks a lot for you help, I’m kind of lost at the third part
that’s what I did so far, but the third part made me confuse little bit

function receiver(commands) {
  for(var i = 0; i < commands.length; i++) {
   if(commands === 'f') {
     console.log(moveForward());
   } else if(commands === 'r') {
     console.log(turnRight());
   } else if(commands === 'l') {
     console.log(turnLeft());
   } else {
     console.log('Error Command')
   }
  }

receiver(commands);
console.log(receiver('f'));

Commands is a stream. If you compare the whole stream to “f”, you’re going to get an error.

What you need to do is compare it one letter/instruction at a time. Best way to do that is to use bracket notation with the for loop as suggested by @camperextraordinaire.

Ohhh, I think I see
if(commands === moveForward) {
console.log(‘f’);
}

would be it right?

No. Based on the information you’ve given us, commands are going to be given in a buches a string like “rffrllfffflfrrfffl”.
You cannot compare a whole string to a single letter and expect to get a true statement. You must compare each letter at a time.

1 Like

is it the commands ?

It’s ‘i’, I’m having hard time sometimes to understand the questions. Maybe it’s cuz the english it’s not my first language. But I do know the problems, and I can see ur trying to helping me thank you so much for that

“commands[i]”
I was focusing more to question than trying to resolve it I guess.

“If these are the only three possible characters, you can use one if statement, one else if statement, and one else to call the applicable functions (goForward, goRight, or goLeft).”
You said I only need to use tree if else statments, and call the applicable functions. You mean call at end at “else”?

function receiver(commands) {
  for(var i = 0; i < commands.length; i++) {
   if(commands[i] === 'f') {
    moveForward();
   } else if(commands[i] === 'r') {
     turnRight();
   } else if(commands[i] === 'l') {
     turnLeft();
   } else {
     console.log('Error Command')
   }
  }

Thank you so much Randell, you’re helping a lot

That’s awesome I just saw it.
when we putting the “f: moveForward” we need to put the parentheses right? Cuz it’s function

It is! Thanks for help Randell you’re awesome.

It’s one of my bootcamps pre-work project