Practice Exercises - console.log & Variables

Thanks Malik,
that cleares question 8 for me, but still got problems with question 9 and 10…

it worked? I got an error when I put your code in (I copy-pasted it)

1 Like

Why do the solutions pasted here, not work when I put it into the console?

It makes me worry that I’ll never learn to code correctly because I don’t know what the correct answer is and there’s no place to look up the correct answer. I know we’re supposed to Google stuff but it doesn’t answer my questions when I don’t know what to search for most times…

2 Likes

I don’t know. So it gives you errors or just a bad result? For me it works, so I don’t know what’s wrong. @Malik or @thecil can you help? :wink:

Now I read that it gives errors. Sorry :grinning:

Yes it gives me this syntax error:

1 Like

I found the problem with this. The quotes from the program. When I copy-paste from atom they were like this (“Input your height in centimeters:”) and they have to be like this : (“Input your height in centimeters:”). I don’t know if you notice, because they are small af, but when you copy-paste into the console, you can see the difference. For the quotes we need to press SHIFT+the apostrophe/quotation mark left from ENTER key.
Also when you have a syntax on the console between quotes it’s going to be colored in red, like you probably saw some of the functions have different colours in Atom.

Hope this will help!

I can see here, the quotes look the same. If you copy-paste from here, change the quotes. :sweat_smile:

1 Like

okay I changed the quotation marks and it worked!!!

You might have figured out why the answers given here don’t work in Console (because I’m supposed to change the quotation marks)! Thanks!

2 Likes

Glad I can help. Good luck!

Have a nice evening! :grinning:

1 Like

Thank you for helping everyone out here. Much appreciated ! :smile:

1 Like

I have a question. So, if we manage to write the code from any exercise in a different way than the solution and it works, is it ok or the solution provided is the only one that is correct?

1 Like

Hey @carmen1408, hope you are well.

Now the solution provided is just a guide line, not the only valid solution.

In code, there are multiple ways to do the same task, so is completely valid to complete the same exercise with a different way, in fact is even better that you solved by your own solution, since you are starting to think/solve by yourself. :nerd_face:

Now just to give you a in-deep with “multiple ways to do the same task” what I could add to it is there are better ways to do the same task, you could have a code method that solve the same issue but it takes lets say 1000 lines of code, then you could come with your own code method that solve the same but in only 100 lines of code, that means that your code is optimal than mine, optimal in: much easy to read/maintain, probably much faster because the cpu will have less code lines to process.

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

Carlos Z.

3 Likes

@thecil thank you for the answer. I want to see if I wrote this ok. I mean it works in the console, but I am still not sure.

>       var total = 0;
>       var input = prompt("Station 1! How many people go on the bus?")
>       var st1 = parseInt(input);
>       total+=st1;
>       if (total > 30) {
>         console.log ("The bus is full.", total-30 ,"must walk")
>       }
>       else {
>         console.log ("Passengers are going up station 1:", st1)
>       }
>       var input = prompt("Station 2! How many people go on the bus?")
>       var st2 = parseInt(input);
>       total+=st2;
>       if (total > 30) {
>         console.log ("The bus is full.", total-30 ,"must walk")
>       }
>       else {
>         console.log ("Passengers are going up station 2:", st2)
>       }
>       var input = prompt("Station 3! How many people go on the bus?")
>       var st3 = parseInt(input);
>       total+=st3;
>       if (total > 30) {
>         console.log ("The bus is full.", total-30 ,"must walk")
>       }
>       else {
>         console.log ("Passengers are going up station 3:", st3)
>       }
> 
>         console.log ("The buss arrived with", total, "passengers on board")

The thing is the code from the solution is not working in the console and can’t figure it out the bug, and there is something in the solution I don’t understand. Why after every if, you have to write that passenger=30?

Hi, I have the same problem, can’t find those quotes on Mac.

Would have been nice if the Academy had mentioned that in their answer. As it was I wasted time wondering what the ‘f’ was going on. It looked like a regular quote button (by the enter key).

1 Like

Hi there,
Regarding exercise 13 I think better solution to what you gave would be:

var x = prompt(“Enter the value of x”);
console.log(“x =”, x);
var y = prompt(“Enter the value of y”);
console.log(“y =”, y);
console.log(“The diference between X and y is”, (x - y));

than the print express which one is x and which one is y, otherwise it prints just numbers and it is not clear for the reader of the print which is which.

1 Like

Re Exercise 14
Is this solution also correct?

var a = “Bitcoin”;
var b = “Ethereum”;
var c = “Chainlink”;
console.log(a);
console.log(b);
console.log( c);

The solution you gave in solutions:

var crypto1 = “Bitcoin” ;
var crypto2 = “Ethereum” ;
var crypto3 = “Litecoin” ;
console .log( Crypto 1: ${crypto1} );
console .log( Crypto 2: ${crypto2} );
console .log( Crypto 3: ${crypto3} );

I noticed new signs and ways of scripting that wasn’t explained in previous lessons or maybe I missed something?

1 Like

Yes, It is correct.

Yes, it is mentioned in the textbook solutions. It is just a different way of console logging.

Hope this helps.

1 Like

My answer to 16

var bread = 2
var milk = 1.5
var cheese = 4
var yoghurt = 1.2
var sum = 0
console.log(“welcome to Ivans shop”);
var amountb = prompt(“how much bread do you want?”)
sum+=breadamountb
var amountm = prompt(“how much milk do you want?”)
sum+=milk
amountm
var amountc = prompt(“how much cheese do you want?”)
sum+=cheeseamountc
var amounty = prompt(“how much yoghurt do you want?”)
sum+=yoghurt
amounty

console.log(“total”+sum);

1 Like