1

I'm just beginning my learning process and am stuck now on this one string. I've searched MDN, Google and Bing and not found any help.

My code instructions tell me to assign a variable, which I did. Then it wants me to test in console.log. I've done so, and when I do with spaces and punctuation, it gives me an error saying that it expected an identifier and instead saw '+'.

If I take the punctuation out I don't have an error, but no punctuation. If I take out the extra spaces as well as punctuation, I get a strange run-on sentence but no errors. I'm working this problem in Udacity, it is quiz 24 of lesson 2.

My code is:

var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " +  adjective1. +  " James and Julia are so " +  adjective2. +  " I cannot wait to work through the rest of this " +  adjective3 +  " content!";
console.log(madLib); 
1
  • remove the . after adjective1 and adjective2 Commented Oct 3, 2017 at 17:01

2 Answers 2

2

You need to add the dots as strings as well.

var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " +  adjective1 + "."
        + " James and Julia are so " +  adjective2 + "."
        + " I cannot wait to work through the rest of this " +  adjective3 +  " content!";
    
console.log(madLib);

The dot . has a special meaning in Javascript. It works as accessor for properties of an object.

Math.floor(1.5); // return the integer value of the given number

Read more here about property accessor.

Sign up to request clarification or add additional context in comments.

Comments

0

Add . (dot) inside the double quoted part of string, not next to the variable.

It is a part of string, not a in-memory variable. And you are calling no function after that.

Below snippet works properly.

var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " +  adjective1 +  ". James and Julia are so " +  adjective2 +  ". I cannot wait to work through the rest of this " +  adjective3 +  " content!";
console.log(madLib); 

Example of using dot to call a function. It is not required in this case since it is already a string.

var adjective1 = "amazing";
var adjective2 = "fun";
var adjective3 = "entertaining";
var madLib = "The intro to JavaScript course is " +  adjective1.toString() +  ". James and Julia are so " +  adjective2.toString() +  ". I cannot wait to work through the rest of this " +  adjective3 +  " content!";
console.log(madLib); 

2 Comments

Thank you!!! Yes I got it to run correct. Now if only the submit for the quiz would let it pass! it keeps telling me the madLib var isn't formatted correctly
What is the output they expect in this variable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.