Practice Exercises - console.log & Variables

you can not use the jump line \n keyword in that way, you can only specify per each line. Also you are using curvy quotes, which is not the correct quotes for programming.

var BTC = "Bitcoin"
var ETH = "Ethereum"
var XTZ = "Tezos"

console.log(BTC)
console.log("\n")
console.log(ETH)
console.log("\n")
console.log(XTZ)

//or just
console.log(BTC, ETH, XTZ)

Excellent answers, It’s easy to understand. Please keep them like that :muscle:

Carlos Z.

Thanks! the first one, putting everything in separate lines, worked :+1:
(the second option didn’t work again, it didn’t put them in separate lines)

this:
console.log(BTC, ETH, XTZ);
Returned this:
Bitcoin – “Ethereum” – "Tezos"

I have another question for someone here please: HOW do you add / calculate the sum of the numbers as variables, if some of the numbers are fractions, as in the grocery list example (so adding 2 + 0.5 + 4 + 1.2). I’ve written my own answer to the original question and I can’t get it to work to return the correct number…

This is my code:
var Milk = 2;
var Eggs = 1.5;
var Cheese = 4;
var Yoghurt = 1.2;

console.log("The total bill for your grocery shopping will be " + "€ " + Milk + Eggs + Cheese + Yoghurt);

1 Like

Your console.log does not sum the prices, just show one after the other, that is the issue with the incorrect sum.

Now to solve it is just easy to create a variable that handle the sum of all other items.
Here is the code but with those few improvement

Carlos Z

thank you!
but your solution also gives the wrong total, it should be 8.7 not that weird thing with two full-stops starting with 21. That is precisely what i’m having trouble with: it doesn’t count 1.5 it counts it as 15.

1 Like

My solution works, because the result is indeed 8.7, which is printed at the end(through the console.log(total), i did not change your code, i just add an extra variable called total which sum the 4 other variables value and then just show total in a console.log().

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

@Marcelle,

Like @thecil said, When you console.log these variables, console.log() takes it as a string. So basically it just combines the letters together. It does not “add” them.

So, If you want to see all your answers together you would do something like this.

var Milk = 2;
var Eggs = 1.5;
var Cheese = 4;
var Yoghurt = 1.2;
var Total = Milk + Eggs + Cheese + Yoghurt //the actual addition happens with numbers

//here below it takes that number and shows it as a string
console.log("The total bill for your grocery shopping will be " + "€ " + Total); 

This way you will be able to see the answer.

If you want to go fancy and do the calculations in the console log statement itself, you need to explicitly mention that these are numbers and they should be added first then displayed as strings. It can be done by putting brackets –

//whatever in the bracket is treated as numbers and added up, then it is displayed as string (basically text) 
console.log("The total bill for your grocery shopping will be " + "€ " + (Milk + Eggs + Cheese + Yoghurt) );

Hope this clears it out.

1 Like

I got a problem with question 8, 9 and 10 of the exercice console.log and variables… I looked at the answers but cant find the solution on my own… regarding the dollarsign and the {} brackets and question 9 and 10 are also a mysterie to me… Can anyone point me in the right direction where I can find the content to answer the questions on my own…

1 Like

Hey @Kim, hope you are great.

Now in the same lesson of the practice exercises, you have a PDF with all the solutions.
https://academy.ivanontech.com/products/javascript-programming-for-blockchain-developers/categories/1707746/posts/12729215

If you have any more questions, please let us know so we can help you! :slight_smile:

Carlos Z.

Hi Carlos,
Sorry but that doesn’t answer my question. I couldn’t solve the questions so I went to the answers… So I already saw them… But that didn’t gave me an explanation of how those answers came about… And yes I already went to the 3 lessons mentioned wwhen having difficulty, but couldnt find the answers in those lessons neither… Its frustrating… hope you can help me… because it’s still early in the chapter and am already bumping into problems… to specify I solved question 8 in a different manner and got the correct output , but the right solution is with the ${}, which I find nothing about in the course and regarding question 9 and 10. I never found a lesson were it’s been taught or even mentionned for that matter about the var prompt and the console.log prompt… All the other questions about the console and console.log were easy so I was wandering if i missed something…

Kim

1 Like

Hi Carmen ,

I have a problem with question 8 also, I can’t find it in the course where they refer to ${}… Can you tell me in wich lesson they talk about it?

kind regards
Kim

2 Likes

Hi @Kim,

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.

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:

2 Likes

Hello,

As Ivan mentioned earlier in the course, some things we have to search ourselves. The best friend of a developer is Google. I don’t think they can explain every syntax from javascript so some we have to look and find them.

Good luck! :slight_smile:

2 Likes

Thank you very much… but I couldn’t find this on my own if you didn’t tell me… searched on google, but without help I wouldn’t have found it… It’s nowhere in the course, is it?

Hi everyone, so I’m struggling learning to code, mainly because when I put my code into the Console, even some of the answers they guys here have provided, the console gives an error message of some sort.

My question is, where do you TEST your code to see if it works (or if you’ve made a mistake) if not in the Console? Am I doing it wrong testing everything in the console maybe?

1 Like

Another one, so whiney today :slight_smile:

How do you get your code to not return ALL your options. For instance, I put the code below into console, it asks for input, I put “170” in. Then it proceeds to give me ALL the answers (you’re a short-arse, you’re too tall, you’re average) in succession, one after the other. Which is not correct obviously.

var Input;
Input = window.prompt(“What is your height in cm?”, “0”);

console.log(Input);

if(Input <= 140);{
alert(“You are a short-arse.”);
}
if(Input >= 190);{
alert(“You are too tall.”);
}
if(Input >= 140 && Input <= 190);{
alert(“You are of average height.”);
}

Hello,

I also test in the console.
For the other problem I think you have to use else if and else for the second and third condition. I did it like this and worked. I am not sure, because I am new to this.

var height = prompt (“Input your height in centimeters:”)
if (height < 140) {
console.log (“You’re low”)
}
else if (height > 190){
console.log(“You’re tall”)
}
else {
console.log (“You’re average”)
}

Have a nice day!

I am having troubles with question number 8. Can anyone help. The answer provided does not work either…

1 Like

The answer from the solution provided is working https://gyazo.com/989277e23127d7d419215a08dfe6311e

Maybe if you wrote the solution and didn’t use copy-paste, you used this sign ’ instead of this one ` . I had the same issue. The right sign you have to use is the one next to 1 in the keyboard.
I did not find the solution from the answers provided by Ivan and the guys, because I didn’t know about ${}. This was the solution I found and I don’t think is wrong : https://gyazo.com/fdb88c486d454ea92f0fce85209e5a66

Hope that helps you.

1 Like

You are correct. Thanks a lot! I was using ’ instead of `. LOL Much appreciated!!!

1 Like