A newbie needs help for his code

Hey guys, i wanted to go further with the exercises lesson 11 and reminder everything i’ve seen so far.
On the last exercise, for the shopping trip, i am trying to make the list and the total price of shopping appear on a webpage.
Actually i can do it, my only problem is when using “\n” to go to the next line.
I am trying to have each product appearing on a separate line and i can’t do it, wherever i put this “\n” sign.
Is my way of coding correct?
Where do i put those “\n” in order to get what i want to? i am struggling since the beginning with those “\n” signs, probably i forgot something

`<html>
<head><h1>Shopping trip !</h1>
</head>
<body>
<script>
var bread = 2;
var milk = 1.5;
var cheese = 4;
var yogurt = 1.2;
var curr = "euro";


var nbread =prompt("How much bread?");
var nmilk =prompt("How many litters of milk");
var ncheese =prompt("how many pieces of cheese");
var nyogurt =prompt("how many cups of yogurt");

var pbread =bread*nbread;
var pmilk =milk*nmilk;
var pcheese =cheese*ncheese
var pyogurt =yogurt*nyogurt;


var total = pbread+pmilk+pcheese+pyogurt;
console.log(total+curr);

</script>
Number of bread :<script type="text/javascript">
   document.write(nbread)
</script>
Litters of milk :<script type="text/javascript">
   document.write(nmilk)
</script>
Pieces of cheese :<script type="text/javascript">
   document.write(ncheese)
</script>
Cups of yogurt :<script type="text/javascript">
   document.write(nyogurt)
</script>
TOTAL : <script type="text/javascript">
   document.write(total+curr)
</script>
</body>
</html>`

Thank you !

Sorry for all of these posts, i was managing to send the code… xD

I think the /n sign for new line is only accepted in the string values. Im not sure how it operates with the document.write function. There is additional explanation on that topic here https://www.w3schools.com/js/js_strings.asp (see towards the middle of the page). There is probably a way implementing it with document.write, but at this level it will maybe be too overwhelming.

If i can add comment to your code, it all looks fine, just the part where the code starts repeating itself (with script elements and script type at the end of the code). Generally we don’t want these repetitions to happen.

Also maybe it will be helpful, to just do the requirements that the exercise is asking you to do. That is to print the total amount you have to pay. But i understand if you want to challenge yourself and create something additional besides the given tasks, im also like that :slight_smile:

1 Like

Indeed i wanted to challenge myself but then i moved forward to booleans :slight_smile:

Thank you for your help i see what you mean with the code repeating, i will fix it. It helps me understand globally how its working.

1 Like

image

This is the printing out to the console and the example how the new line is used. I think you understood it correctly, just that where and how this is used is not always the same.

Best,
Peter

2 Likes

Thank you for your help i understand my mistake now :slight_smile:

1 Like