Return and console.log do not work as expected in Codecademy

2

I am doing Javascript course on Codecademy , I did the function, it is returned what the exercise asks for, but I do not step.

    
asked by anonymous 25.06.2015 / 01:06

2 answers

4

The return is an instruction for the function, it has no connection with console.log .

The console.log is a method that was "idealized" recently (when there were already javascript and browsers) to debug the scripts, you do not use it for "production" but for development.

The correct would be to "return" using return and capture this return with console.log

return works like this

function test(){
    return "test";
}

console.log(test());

So the code should be:

function nameString(name)
{
     return "Oi, eu sou " + name;
}

console.log(nameString("Allan"));
console.log(nameString("Susie"));
console.log(nameString("Fred"));

Note that the code runs until before return , but what comes after does not run:

function nameString(name)
{
     alert(1);//Isto será executado
     return "Oi, eu sou " + name;
     alert(2);//Isto NÃO será executado
}
    
25.06.2015 / 01:11
4

You have to fix 2 things:

  • The function must give return
  • the string you return must begin with large letter

The code academy is a Robot, you have to do as he wants.

# 1 - Note that your function should have return instead of console.log . What is wanted is for the function to return and then make console.log of the invocation of the function (of its return).

What you want is return "Oi, eu sou" + " " + name; and not console.log("Oi, eu sou" + " " + name);

Then you can do console.log(nameString('Sergio')); or so var return = nameString ('Sergio'); console.log (return); '

# 2 - Notice that oi and Oi are different strings . you have to start with large print.

The code as it should be:

var nameString = function (name) {
    return "Oi, eu sou" + " " + name;
};
console.log(nameString('Sergio'));
    
25.06.2015 / 01:16