5

The following code is meant print the namestring with a name. however, it's not correct.

var nameString = function (name) {
    return "Hi, I am" + " " + name.

}
nameString("Amir")
console.log(nameString)

What am I not implementing/doing wrong that stops it from displaying the string as well as a name? thanks.

user3848245
  • 73
  • 1
  • 1
  • 3

6 Answers6

7

First mistake in your code is in the line

 return "Hi, I am" + " " + name.

remove fullstop or just concatenate it as below

 return "Hi, I am" + " " + name+"."

and then write

console.log(nameString("Amir"));

check it here fiddle

Roshan
  • 2,144
  • 2
  • 16
  • 28
  • the fullstop was throwing the entire code off, thanks for pointing it out, and now it's fixed. Beginners mistake that i will remember for next time, thanks again! – user3848245 Jul 17 '14 at 08:46
  • I still have to wait a few minutes, before I can accept an answer. I'll do it as soon as I can. – user3848245 Jul 17 '14 at 08:50
2

you will have to call that function.

console.log(nameString("Amir"));

If you will say console.log(nameString) it will just print the value of varibale nameString which is a function.

There is a . in your function in last of return statement, remove that.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • thank you as well for pointing out the full stop in the return statement, beginners mistake and it won't happen again. thanks! – user3848245 Jul 17 '14 at 08:47
2
console.log(nameString())

just forgot to CALL the function. and pass params

console.log(nameString('anyString'))
Gena Moroz
  • 929
  • 5
  • 9
1

nameString is a method and you are not passing argument to it

console.log(nameString("Amir"));

or

var str=nameString("Amir");
console.log(str);

and also remove .

Govind Singh
  • 15,282
  • 14
  • 72
  • 106
0

var nameString = function(name) {
  return "Hi, I am" + " " + name
  nameString("Sophie");
}
console.log(nameString("Sophie"));
0

You can use template literal, which makes the code more readable and makes your job easier. You don't have to think about spaces and there are fewer inverted commas.

var nameString = function (name) {
  return `Hi, I'm ${name}.`;
};
nameString("Amir");
console.log(nameString("Amir"));
TomDev
  • 285
  • 2
  • 13