JavaScript 101: Input and Output code problem

Hi guys,

I am following the JavaScript 101 course with Szolt and I have a problem with the code of inputs and outputs when he uses the console.

His text is basically about adding constant values and he adds a crypto ticker and a number to buy. After he does this, he runs the console by trying to display the message: You have just bought 1.15 BTC. I checked his code ten times to see if my identical code has mistakes but it seemingly does not have any mistakes and is identical to his. However, in his version the message displays as intended while my result is: You have just bought ${amount}${ticker} . Can someone explain to me what I am doing wrong? Please find my code below:

const ticker = prompt ('Enter your crypto ticker: ');
const amount = +prompt ('Enter the amount you bought: ');

console.log('You have just bought ${amount}${ticker}');

So, if I add the ticker BTC and 1.15 I should get the message " You have just bought 1.15 BTC". Instead I get "You have just bought $(amount)$(ticker)

Not sure if this matters but I am using Brave as browser and MAC as operating system.

Thank you for the help.

you should use `` instead of ‘’ when using variables in your console.log

2 Likes

Thank you Pyjama man, the problem has been solved! May your pyjamas be forever comfy!

1 Like

Hello, It might be a similar situation for me. On the first excercise. I have the same code he types and although the prompt window comes up, it doesnt return the input name as output rather it shows the code.

let name = prompt ('Enter your name: ');
alert (‘Greetings, ${name}!’);

The second prompt that shows up comes out like:
Greetings, ${name}!

Can someone help me?

@Pyjama_Man

yes you have the same problem should use `` instead of ‘‘ so the code should be

let name = prompt ('Enter your name: ');
alert (`Greetings, ${name}!`);
1 Like

Ok great, tried it out and all groovy. Can you tell me why we use back tick for Greetings since other type of quotes, " ’ ¨ seem to work fine to define a string, isnt the second part a string as well?

when using dynamic values (variables) you need to use it to tell javaScript to read data from variable and you put variable in ${} so javaScript knows where the variable is otherwise javaScript will think it is just a string

1 Like

Sorry on the late reply, Ive been digging into this and reading my Eloquent Java Script book. Awesome! Thank you for your helpful reply.

1 Like