Practice Exercises - console.log & Variables

Hello,

I am doing my exercises and I have a small problem. For the exercise no. 8 I wrote the code like this and the result was the one that I expected.
https://gyazo.com/fdb88c486d454ea92f0fce85209e5a66

The solution gave me this result https://gyazo.com/9faac5bf20b833a5417791836dfc10fa

Can you explain if I did something wrong or what is the solution about?

Thank you :slight_smile:

3 Likes

Hi there,

I was faced with a similiar problem and found out that in order for ${} to work, the whole statement has to be inside of quotes that look like ``.

I cannot find the shortcut for those quotes on my keyboard so i have to always copy paste them. Can someone please explain how to find the right shortcut for this quotes on my keyboard?

Thanks :slight_smile:

4 Likes

Oh, get it now! yeah with those it works. The shortcut for the quotes `` is just the to press the first key that is next to “1”. It also makes this mark when you press also “shift”: ~ .
Thank you very much and have a great day!

4 Likes

Great, im glad it helped :slight_smile:

I found them! I was having my keyboard set to qwertz instead of qwerty and now i can press it :slight_smile: ´´ `` ^^ yeah!! :smiley:

1 Like

Hei,

I have another question if it’s not too much. At the exercise no.16, the one with the shopping list I made this program :

      var bread = 2;
      var milk = 1.5;
      var cheese = 4;
      var yogurt = 1.2;
      console.log("Hi! Welcome to the Ivan on Tech shop!");
      var quantBread = prompt("How much bread do you want?");
      console.log ("How much bread do you want?");
      console.log (quantBread);
      var quantMilk = prompt("How much milk do you want?");
      console.log ("How much milk do you want?");
      console.log (quantMilk);
      var quantcheese = prompt("How much cheese do you want?");
      console.log ("How much cheese do you want?");
      console.log (quantcheese);
      var quantyogurt = prompt("How much yogurt do you want?");
      console.log ("How much yogurt do you want?");
      console.log (quantyogurt);
      var totalamount=(bread*quantBread)+(milk*quantMilk)+(cheese*quantcheese)+(yogurt*quantyogurt);
      console.log("The total amount to pay is ", totalamount);

But the solution is different from the one I thought about. The resullts are the same for the both of them, but I want to know for sure if the code I wrote is ok too.

3 Likes

Well, honestly i just started exploring and learning about coding also, so i am not maybe the best person to review your code and say if it is good or not :slight_smile: its, working and for beginning that is a good sign :smiley: But if i can comment the code, i would say the console.log are to many, as only the last one, which prints the total amount to the console, is required. By removing these, the code will immediately look cleaner.

In regards to the solution, mine code is also not always similar to the presented solution, but in my opinion if you can understand and read the given solutions and learn from that as to what you could improve on your side, or what is maybe better on your side, this is what matters. We are here to learn :slight_smile:

Best,
Peter

1 Like

Thank you so much for your time and answers.

All the best!

1 Like

Yea that would work this is how i did… excuses my language it was late last night…lol

var milk = Number(prompt(“This is Dans sht\nGot Milk?"));
if (!Number.isNaN(milk)) {
console.log(milk * 1.5);
} else {
console.log(“How Much Milk You Want Fool?”);
}
var bread = Number(prompt("This is Dans sh
t\nYou Still Want Dat Bread?”));
console.log(bread * 2);

var cheese = Number(prompt(“This is Dans sht\nShould I Throw Some Cheese On Dat Btch?”));
console.log(cheese * 4);

var yogurt = Number(prompt(“This is Dans sh*t\nYou Messin Wit Dat Gurt”));
if (!Number.isNaN(yogurt)) {
console.log(yogurt * 1.2);
} else {
console.log(“Talkin Bout Dat Yogurt, I pitty a Fool!”);

if (!Number.isNaN()) {
console.log(alert(" Come On Now, Enter a Number Amount!"));}
}

console.log(alert((milk1.5)+(bread2)+(cheese4)+(yogurt1.2)+“€”));

I don’t know who i should let know about this but one of the Achademy’s code is not working properly in the solutions for exercise #16. There should be no ‘s’ in Yogurt"s" at the bottom of the code. It’s giving a Undefined response in the console for Yogurt.

var bread = 2 ;
var milk = 1.5 ;
var cheese = 4 ;
var yogurt = 1.2 ;
var sum = 0 ;
alert( “Hi! Welcome to the Ivan on Tech Shop!” );
var input = prompt( “How much bread do you want?” );
sum += input * bread;
var input = prompt( “How much milk (liter) do you want?” );
sum += input * milk;
var input = prompt( “How much cheese do you want” );
sum += input * cheese;
var input = prompt( “How many yogurts do you want?” );
sum += input * yogurts;
console .log( “The total amount to pay is” , sum);

1 Like

For me is working. I mean, yeah, is a typo mistake, but if you delete the “s” from “ yogurts” it’s working.

1 Like

Like @carmen1408 picked out, you need to remove “s” from your “yogurts” variable as you defined “yogurt” and not “yogurts”.

Happy Learning! :smile:

3 Likes

Ok so im completely lost here. The instructions tell us to create a file and save it in js format but when i do that i cant bring it up in fire fox so what ive been doing is just opening up the console in a random page and doing the excersizes there, i havnt been able to save them and im worried because i think we may have to build on these in lessons that follow. I dont know if atom has a console in it or how to use it. Why are they asking us to save it in js format? I guess i just need help using atom but specifically bringing up the console in atom and saving in js file format

1 Like

I think they said to save it in js format so we know what problem it is and not to write a line of code in the console at a time and to copy-paste all program in the console when it’s done.

So we write it in java and copy it into the console, got it. I guesse the point is for us to learn the excersize and its not like were going to turn it in or use it in future lessons.

I am not sure. That is what I think. @Malik should tell us for sure.

Hi @Moneymonty and @carmen1408,

Hope you’re having a great day.

You won’t see console logs in your atom console. The only way you can see it is if you run a local node server and console log through it which is out of the scope of this course.

If you have your js file with all your javascript code, you need to place it in a HTML file and then open the HTML file in your browser. Thus, no matter how big your Javascript code is, we can see all the outputs on the chrome console.

Find below code for reference

My HTML –

<html>

<head>
    <title>This example of using form to add item to array comes from IvanOnTech JS course.</title>
    <script src="/Users/malik/Downloads/myJscode.js"></script> //your js file location

</head>

<body>

    <h1>My favorite fruits</h1>

    <input class="fruitInput" type="text" placeholder="add fruit">
    <button>Add</button>

    <ol class="fruitList">
    </ol>
</body>

</html>

My JS file (myJscode.js) –

const listValues = [120, 4, 9, 7, 2, 8, 3, 1, 1100];
var lowestValue = listValues[0];
var highestValue = listValues[0];
// console.log(lowestValue)
for (num in listValues) {
    num = Number.parseInt(num)
    if (listValues[num] < lowestValue) {
        lowestValue = listValues[num]
    }
}
console.log(lowestValue)

for (num in listValues) {
    num = Number.parseInt(num)
    if (listValues[num] > highestValue) {
        highestValue = listValues[num]
    }

}
console.log(highestValue)

So, the conclusion is, no matter how big the project, in the end, we always inject our JS code into the HTML code for the browser to read it and execute it. The only way we can execute JS files outside of HTML and browser is through a node server instance (you will learn it in advance courses).

Hope this helps. Happy Learning! :smile:

2 Likes

Man i was way off on that 1. I forgot u csn use to actually put kava in the html code. Can u imagine trying to do these assignments without knowing that? Lol

I need help please: it’s Question 14: " Text printing with formatting .

  1. Write a script that contains three different cryptocurrencies contained in three different variables.
  2. Print the cryptocurrencies in 3 different lines."

I want to console to look like this:
Bitcoin
Ethereum
Tezos

I CAN NOT SEEM TO GET THE F***ING CODE TO WORK AND GO INTO A NEW LINE. WHAT AM I DOING WRONG?

This is what I’ve got:
var BTC = “Bitcoin”
var ETH = “Ethereum”
var XTZ = “Tezos”

console.log(BTC, \nETH, \nXTZ);

If I put the variables into inverted commas it works, but then it returns the actual tickers (the variable names, not the values of the variables)

1 Like

Hello @Marcelle, hope you are great.

Could you please share your code? so we can review it to help you solve the issue.

You can use the “Preformatted Text” Button to encapsulate any kind of code you want to show.


function formatText(){

let words = “I’m a preformatted Text box, Please use me wisely!”

}

prefromatted_text-animated

Carlos Z.

1 Like

var BTC = “Bitcoin”
var ETH = “Ethereum”
var XTZ = “Tezos”

console.log(BTC + \nETH + \nXTZ);

1 Like