Getters and Setters III problem with ${}


#1

Hello, I am on Getters and Settes III and the exercise itself is not a problem. The app allowed me to progress to the next exercise, despite my output being different. Basically, every time I try to print a string with a var inside it like for example:

console.log('this is the ${varNumber} time I have tried to do this and failed') 

It never prints the value but instead simply the string

'this is the ${varNumber} time I have tried to do this and failed'. 

An example from the exercise:
Capturedsdsdfs

I can see that it is not being recognized because the colouring of the code is not correct, the interpreter thinks the whole things is a string, unlike in the example bellow, but I have no idea why.
examp

Does anyone know how to fix this?


#2

Hi @terrareginia,

If you look closely at the pic you provided, take note at the console.log:

console.log(`Change ${newCapacity} to a number.`)
            ^                                  ^

The example is using backtick symbol `

Whereas, in your code, you’re using single quote ’ ’ symbol

console.log('this is the ${varNumber} time I have tried to do this and failed') 
            ^                                                                ^

This is the new ES6 syntax, to use ${variable} with strings, backtick symbol ` is used, you may refer the documentation here (called template literals/template strings)

So for your code, change all them into something like this, it will print correctly this time:

console.log(`${this._name} is ${this._age} years old`); //using backtick

Cheers :slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.