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') {
   } else if(turnRight() === 'r') {
   } else if(turnLeft() === 'l') {
   } else {
     console.log('Error Command')


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':
		case 'N':
		case 'S':

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

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.

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') {
   } else if(commands === 'r') {
   } else if(commands === 'l') {
   } else {
     console.log('Error Command')


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.

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?

Ohhh, I think I see
if(commands === moveForward) {

would be it right?

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

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 ?

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?

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

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.

I was focusing more to question than trying to resolve it I guess.