Function for receiving commands

Function for receiving commands
0

#1

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


#2

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


#3

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

#4

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.


#5

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?


#6

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


#7

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


#8

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).


#9

First, it said your function will receive a list of commands. For your function to receive something, it is best to use parameter(s) in the function definition. Add a parameter which will be a placeholder for the string that will be passed into the function. To make it clear what the string represents/contains, I would name this parameter commands.

Second, inside the receiver function, you will then iterate over the string passed into it (the argument). Currently, you are iterating over a variable named rover I do not see a rover variable anywhere in your code, so I assume it is some global variable? It really does not matter, because the instructions do not mention iterating over the rover variable to accomplish anything. Focus on iterating over the commands string passed into the function.

Third, inside the for loop, your if statements should be checking if the current string character being iterated over in the commands string is an ‘f’, ‘r’, or ‘l’, Reference the previous paragraph for what the string is. 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). Do you know how to reference a specific character in a string? If not, you should refer to the FCC challenge named Use Bracket Notation to Find the Nth Character in a String.


#10

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'));

#11

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 @RandellDawson.


#12

As @zapcannon99 points out, each of your if statements is comparing the entire commands string to either ‘f’, ‘r’, or ‘l’. To reference a single character of the string, you need to use it’s index. Review the link I posted previously to the FCC challenge for how to make this reference.

Which variable in the for loop do you think contains the current character’s index?


#13

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

would be it right?


#14

moveForward is a function and commands is a string. They will never be equal. Answer my previous question.


#15

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.


#16

is it the commands ?


#17

No, commands holds the entire string. An index is a number and if you did not know this already, you really should go back through the basic JavaScript section again, because this is first level stuff.

Which variable in the for loop contains a number value?


#18

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


#19

Now you know that i contains the current index of the commands string, then how do you reference the current character in commands using the index. If you do not remember, review the following FCC challenge.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-nth-character-in-a-string/


#20

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