The following code throws an error in the browser console
sayHello(firstName, lastName){
let msg = "Greetings ";
function intro(){
return
msg + firstName = " " + lastName;
}
return into();
}
sayHello("Professor" , "Falken");
//returns "Greetings Professor Falken";
After the comment lines is what should come out. However an error is declared, indicating that the first key is an unexpected token. I need an explanation to correct the code.
you need to indicate "function":
should be:
this part will not return anything because you are putting an empty return before doing any operations.
An example to do what you propose in a simpler way would be:
I hope I've helped,
Greetings.
There are several bugs in that code.
The first is that you declare the called function
intro
but then call it with the nameinto
.The second error is the way you are trying to create the message.
The correct way to do it is like this:
msg = msg + firstName + " " + lastName;
I mean, there you tell it that the new value of
msg
must be what it currently hasmsg
("Greetings") concatenated withfirstName
then with the space (" ") and finally with thelastName
.The third error, as they already told you, is that the word
function
a is missingsayHello
.This is important, since when declaring a function you always have to use the word
function
or, failing that, the arrow function() => {}
. Then when you call the function there, it is not necessary to add that and you call the function only with its name and the parentheses.The fourth and last mistake, which has also been flagged for you, is to make sure you put what you want to return in a function always next to the
return
, never below. If not, by putting it down, youreturn
will end up with it returning a valueundefined
.Run the following fixed code and you will see the result you were expecting:
PS: Please consider marking an answer "accepted" in case it was helpful to you. This will help others who have problems similar to yours know that an answer is effective. Here's how to do it .