I need help with JavaScript

Forgot, I added some text to your “victor” div so I knew where it was…

Hi Guys,

Thanks for the help I’ve been getting thus far. I now understand how to input variables into the consul, but I can’t get the program to give me the total cost of items for the grocery assignment. How do I prompt the program to respond directly to the number of items I input? So that in the end it tells me exactly how much I have to pay in accordance with the number of items I put in? I tried printing the pdf solution to the question in Javascript and the program didn’t respond. Any suggestions?

Hi

Not working for me,only getting the input numbers not the sum. But the equation works for, multiply- minus and divide, frustrating !!!

Have you got a line that adds the items you have or are inputting and another that outputs the result?

a = 2;
b = 3;
c = a + b;
console.log("Total: ", c);

Something like that?

Do you have a line that adds the individual items and then outputs the total?

Something like this?

a = 2;
b = 3;
c = a + b;
console.log("Total: ", c);
1 Like

I need help here. I wrote the same console.log code but one of them says syntax error… why?

huh.

Is it because you have used a decimal point in the first example and a comma in the second? 10.101 vs 10,101?

1 Like

This was my input (as was on the PDF solution) but it doesn’t seem to be working.
Anything I’m doing wrong?

Thanks

You are declaring ‘sum’ in more than one place. I’m not sure why you have this line?

var sum = "bread" + "milk" + "cheese" + "yoghurt";

That appears to concatenate a series of strings but later you try to add the ‘input * bread’ to it. You are adding a float to a string probably creates a string with a 2 on the end of it.

Screenshot 2020-10-28 at 17.36.21

Also on line 21 you reset sum to 0. So you are probably getting the cost of yoghurt only at that point.

Would suggest change line 11 to

var sum = 0;

And remove line 21.
I think you also should change lines 5 thru 8 to floats instead of strings. Though when I check in the console what is happening is that the price is being "coerced’ to a float when you multiply it by an integer.

Screenshot 2020-10-28 at 17.41.58

‘bread’ starts off as a string but when sum is made equal to 1 x bread it is no longer a string. Same happens when it’s multiplied by 3. Javascript is not a “strongly typed” language in that a variable can change it’s type and be changed or coerced to a different type depending on what you do. As a result it is easy to be caught out. In your code you are ending up with a string where you want a number and a result you’re not getting a sum.

Hope that helps.

1 Like

Ye but this still gives…
…var a=prompt(“Insert the value of bread;”);
var b=prompt(“Insert the value of Milk;”);
var c=prompt(“Insert the value of Chees;”);
var d=prompt(“Insert the value of Yougrt;”);

var e=a+b+c+d; …a=1…b=2…c=3…d=4
console.log(“Total”,e);…Total=1234 but not 10

That’s because a, b, c and d are strings/text and you have only concatenated them in your code.

"1" + "2" + "3" + "4" = "1234"

Screenshot 2020-10-28 at 20.32.52

I have set bread, milk, cheese, yoghurt as strings/text using the values in your first code and then added them and we get 21.541.5 which is not their sum. It’s just the text.

The value you are getting from the prompt is also a string or just text. a is “3” and not the integer/number 3.

Below I have set the cost of a loaf or bread and litre of milk to the numbers 2 and 1.5. Then get the number of loaves and litres of milk which are both strings/text e.g. “2” and “3”. Then multiply the cost by the quantity e.g. “2” x 2 for bread which ends up as 4 and not “4”. Same for milk “3” x 1.5 which becomes 4.5 and not “4.5”.

I can add those together to get the total as a number and not text

total_bread + total_milk = 8.5 and not “44.5”.

Screenshot 2020-10-28 at 20.44.32

In your last problem/example if you multply a x 1 (or any number) it will be the number 1 and not the text “1”.

Javascript converts a to a number which as it is “1” works. If a were “bread” that doesn’t make sense and gives an error if you try to multiply it but if you add it to something you get text.

Can’t multiply the text “bread” by 2 so get Nan which is an error or “Not a Number”

If try to add a number to a string end up with another string “bread” + 2 = “bread2”.

Screenshot 2020-10-28 at 20.58.20

But

Screenshot 2020-10-28 at 21.04.24

and in fact you can get away with multiplying two strings together

Screenshot 2020-10-28 at 21.17.17

“1” * “2” does output the number 2 so you can use bread = “2”, get the number of loaves as “3” and as long as you multiply them in your script you will get 6 but if you only add them you will get “23”.

Hope I haven’t created more confusion.

2 Likes

Thanks
By adding the numbers of each I get multiplication, number to string and all OK … jeppe

var x=prompt(“Insert the value of Bread:”);

var a=prompt(“The number of bread:”);

var y=prompt(“Insert the value of Milk:”);

var b=prompt(“The number of milk:”);

var z=prompt(“Insert the value of Chees:”);

var c=prompt(“The number of Chees:”);

var s=prompt(“Insert the value of Yogurt:”);

var d=prompt(“The number of yogurt:”);

var e= (xa+yb+zc+sd);

console.log(“Shopping tour price:” + “€” + e);

3 Likes

i put the solution

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);
VM403:14 Uncaught ReferenceError: yogurts is not defined
at :14:16

and it still does not give a total… help

wow. thats the reason why?

Hi, Reggy2323.

The comma is not a valid input for numbers.

Example:
3.14 is valid with numbers in JavaScript.
3,14 is not valid with numbers in JavaScript.

You can learn more about numbers in JavaScript by reading here JavaScript Numbers.

I hope this helps you understand the exercise.

Ivo

1 Like

Hi, Reggy2323.

Your error message says:

VM403:14 Uncaught ReferenceError: yogurts is not defined
at :14:16

When we read this error message, it tells us that:
yogurts is not defined at :14:16
This means that in your code line number 14, you have written yogurts wrong. :wink:

The great thing about error messages is that they tell you exactly where your mistake is in your code.

I will go through it with you, so you are able to see what you did wrong, by reading and understanding the error messages you will get in the future. :wink:

So back to the error message, we can split it up into two parts.
The first part is the “Uncaught ReferenceError”, this will tell you that it’s an error with a reference in your code.
VM403:14 Uncaught ReferenceError:

The next part tells you exactly what reference is wrong, and at exactly what line in your code the error is occurring.
yogurts is not defined at :14:16

So, in your case, it is just the word yogurt that is spelled wrong at line 14 in your code.

I would highly recommend you take the time to read the link below, as it will teach you all about reading the error messages we get when programming in JavaScript.
Link:
What went wrong? Troubleshooting JavaScript.

There is also nothing wrong with rewatching some of the videos if you are unsure on how things work. :innocent:

Good luck, my friend.
Ivo

2 Likes

What is the difference? It seems to result the same thing.

console.log(Crypto 1: ${crypto1});
console.log("Crypto 1: " + crypto1);

It will in the console. The ${a_variable} syntax is needed in a HTML file to output the value of the variable as part of another string.

Here’s a link

String interpolation

1 Like

Hey Guys. Thanks for the help. So I’ve managed to get the arithmetic for the most part. But I can’t get the consul to give me the total amount of bread and cheese. This is a modified version of the exercise that I did. How do I get the program to add the total cost of bread and the total cost of cheese? How should it be written?

1 Like

Thanks for that. I managed to figure out how to get JavaScript to do the arithmetic , but now I’m having trouble to get it to actually say "The total amount is sum. I tried using the PDF solution but it doesn’t seem to work. So now I can only get the program to answer in a crude way and not interactive for a user. Sorry I’m making slow progress on this, but I’m getting there.