Console.log exercises

Hi, I am struggling with exercise No. 8 - print with text formatting

when i follow the solution, console will not give me back values but only literally ${length} and ${width} in text.
Here are all 3 inserted lines:

var length = 10.101;
var width = 3.843;
console.log(‘the rectangle is ${length} cm long and ${width} cm wide’);

and result i got:

the rectangle is ${length} cm long and ${width} cm wide

what am I not understanding? ( iam completely new to programming:)

1 Like

Hi mate !
I just finish this one, actually i struggle a lot because characters seem to be the same but in fact they are not ( i struggled during 2 hours to make a “go to the line” command because i used “/” instead of “”
I copy you the way that worked for me, but i am too noob to explain you exactly why, maybe those " ’ " characters were not the same we both used for coding, i copied them from a code line on the following link


I hope i helped you :slight_smile:

My answer :
var length = “10.101”
var width = “3.843”
console.log(the rectangle is ${length} cm long and ${width} cm wide)

2 Likes

yeah, change of those ’ for ` helped!
dont even know how to name them:)

thanks!

I’m stuck on the same problem. Even after changing my variable to string I get the same result.

var length = 1.101;
var width = 3.843;
console. log( The rectangle is $ {length} cm long and $ {width} cm wide.) ;
VM2033:3 The rectangle is $ {length} cm long and $ {width} cm wide.

1 Like

Hi @CC123,
The ${} syntax is applicable for the new syntax in javascript after 2015. It is called “Template Strings” in Javascript. They let you add variables in the string directly without + . It just a different way of writing strings. If you notice ,template strings make use of backticks instead of normal quotes.

Also, there shouldn’t be any space between the dollar sign and the curly brackets. That’s the mistake you have done.

For example –

Normal string syntax
console.log("my name is" + myVariable)
Template String
console.log(`my name is ${myVariable} `)

Hope this clears it out for you.

Happy Learning! :smiley:

1 Like

Amazing thank you, I thought those were single quotation marks instead of backticks.

HELP!
Here is my console result…not good for question 4
Whare did i go wrong?
Hello world=“hello world”
VM92:1 Uncaught SyntaxError: Unexpected identifier
var hello=“hello”;
undefined
var world=“world”;
undefined
console.log(hello+ " “+world);
VM296:1 hello world
undefined
var note=”!";
undefined
console.log(hello+ " " + world+note);
VM488:1 hello world!
undefined
var age=“age”;
undefined
var note 1=":";
VM572:1 Uncaught SyntaxError: Unexpected number
var noteb=“4”;
undefined
console.log(hello+" "+world+note / age+note 1+noteb);
VM808:1 Uncaught SyntaxError: missing ) after argument list
var age=“age”;
undefined
var old=“age:4”;
undefined
console.log(age);
VM894:1 age
undefined
var age=67:
VM922:1 Uncaught SyntaxError: Unexpected token ‘:’
console.log(“myage”, age);
VM1002:1 myage age
undefined
console.log(“myageis”, age);
VM1086:1 myageis age
undefined
console.log(“my age is”+ age);
VM1171:1 my age isage
undefined
var age+99;
VM1199:1 Uncaught SyntaxError: Unexpected token ‘+’
var My age=“my age is”;
VM1255:1 Uncaught SyntaxError: Unexpected identifier
Console.log(my age+ age);
VM1325:1 Uncaught

Hi, where do you find those quotes on Mac?

Hi there.
In the exercise number 8 I have done this way:

var length = 10.101;
var width = 3.843;
console.log("The rectangle is " ,length, "cm long and " ,width, “cm wide.”);

is this way a wrong way?
It runs properly.

No, it is not. It is perfectly applicable.

1 Like

Hi @Jorenso,

Those quotes are called back ticks and they can be found on a mac as shown below. You shave to hold Shift and type the button.
image

Hope this helps.

1 Like

Thx! That helped a lot! :wink:

Hi! Could you help me with one more thing? How to do line break, when writing code in Crome browser…

This should do the trick - console.log("hi \nJorenso");

The \n will break the line.

1 Like

I wonder if someone could help with the explanation of why you use floating variables? ($) i have searched online but i need an explanation in simple English?

Any help greatly appreciated.

1 Like

What do you mean with floating variables? Could you give us an example please.
Are you talking about JavaScript?

1 Like

Sorry I probably didn’t explain correctly what im talking about is why we use a $ symbol?

image

I can get the same result below:
image

1 Like

Hey @Crypto85, @zero0_cero, hope you guys are great.

The ${} notation: They allow for both multiline strings and string interpolation.
Example:

const one = 1;
const two = 2;
const result = `One add two is ${one + two}`;
console.log(result); // output: One add two is 3

Carlos Z

1 Like

Thats right u get the same result.
The meaning of using that form to write is that you can write and use variables. for example.

var x = 1;
var y = 3;
console.log("hi with this little program we are going to add 2 numbers " + x + " is going to be the first number and " + y + " is going to be the second number and the sum is " + (x+y));
in this way im using a lot of “” and + and is anoying
in the other way we can just put once this reverse quotes `` and when want to show a varible use the ${} tool. Like this.

console.log(hi with this little program we are going to add 2 numbers, ${x} is going to be the first number and ${y} is going to be the second number and the sum is ${x+y});

less quotes and less +, faster the writing just it.

Hope this explanation helps you.

2 Likes

Yes this helps massively. Thanks @zero0_cero & @thecil for all your help.

Matt

3 Likes